我有一个带有一些可变参数 params 的函数,我需要在其中调用另一个传递此参数的函数。
比如有人调用这个函数:
bool A(const char* format, ...)
{
//some work here...
bool res = B(format, /*other params*/);
return res;
}
bool B(const char* format, /**/, ...)
{
va_list arg;
va_start(arg, format);
//other work here...
}
我需要知道,如何通过 A 到 B 函数接收的椭圆传递变量参数。谢谢
您不能直接这样做,因此您需要遵循 C 库使用 fprintf
/vfprintf
函数组所遵循的相同模式。
这个想法是将实现放在v
前缀的函数中,并使用没有v
前缀的面向用户的函数来“展开” va_list
在调用真正的实现之前。
bool A(const char* format, ...)
{
//some work here...
va_list arg;
va_start(arg, format);
bool res = vB(format, arg);
va_end(arg);
return res;
}
bool B(const char* format, /**/, ...)
{
va_list arg;
va_start(arg, format);
bool res = vB(format, arg);
va_end(arg);
return res;
}
bool vB(const char* format, va_list arg) {
// real work here...
}