问题描述
我有两个可变参数函数为美孚(格式,...)
和栏(格式,...)
。我想要实现的功能富
,以便它可以调用栏
同它的参数相同的列表。即,
I have two variadic function as foo(format, ...)
and bar(format, ...)
. I want to implement function foo
so that it can invoke bar
with the same list of arguments it has. That is,
foo(format...)
{
...
bar(format, ...);
}
例如,调用富((二),1,2)
将调用栏
与相同参数栏((二),1,2)
。应如何富
函数来实现?
For instance, invoking foo("(ii)", 1, 2)
will invoke bar
with same arguments bar("(ii)", 1, 2)
. How should this foo
function be implemented?
PS:函数栏
是从传统的图书馆,我不能改变它的界面
PS: function bar
is from a legacy library which I cant change its interface.
推荐答案
能不能做到,只要你有一帮,如果功能与 ...
参数。
Can't be done, as long as all you have is a bunch if functions with ...
arguments.
您必须提前计划这样的事情,并在两个上演时尚实现每一个可变参数fuinction
You have to plan ahead for things like that and implement each variadic fuinction in two-staged fashion
void vfoo(format, va_list *args) {
/* Process `*args` */
}
void foo(format, ...) {
va_list args;
va_start(args, format);
vfoo(format, &args);
va_end(args);
}
一旦你拥有的每通过一对的va_list *
功能和 ...
函数的实现你的可变参数的函数,你可以使用的va_list *委托电话
的功能版本的
Once you have each of your variadic functions implemented through a pair of va_list *
function and ...
function, you can delegate the calls using the va_list *
versions of the functions
void vfoo(format, va_list *args) {
...
vbar(format, args);
...
}
这篇关于要调用一个可变参数函数与其它可变参数函数的参数unamed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!