Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c+中处理va#u参数+;_C++_Function_Arguments - Fatal编程技术网

C++ 在c+中处理va#u参数+;

C++ 在c+中处理va#u参数+;,c++,function,arguments,C++,Function,Arguments,我有一个函数a(…)和B(…)。现在我必须在A内部调用B,是否有方法将从A传递到B?伪代码: void A(...) { // Some operators B(...); // Instead of ... I need to pass A's args } p、 我知道这可以用宏来完成,但是函数呢。你不能转发va_参数。您只能转发va_列表 void vB(int first, va_list ap) { // do stuff with ap. } void B(in

我有一个函数
a(…)
B(…)
。现在我必须在
A
内部调用
B
,是否有方法将
A
传递到
B
?伪代码:

void A(...)
{
   // Some operators
   B(...); // Instead of ... I need to pass A's args
}

p、 我知道这可以用宏来完成,但是函数呢。

你不能转发va_参数。您只能转发va_列表

void vB(int first, va_list ap)
{
   // do stuff with ap.
}

void B(int first, ...)
{
   va_list ap;
   va_start(ap, first);
   vB(first, ap);
   va_end(ap);
}

void A(int something_else, int first, ...)
{
   va_list ap;
   va_start(ap, first);
   vB(first, ap);       // <-- call vB instead of B.
   va_end(ap);
}

不幸的是,您不能将
va列表
传递给接受变量参数列表的函数。没有语法允许您将va_args结构扩展回参数


您可以在单个参数中将其作为
va_列表
传递,因此一种解决方案是将执行该工作的函数定义为获取
va_列表
。然后,您可以定义另一个使用参数列表包装它的函数,该参数列表可以将
va_列表
传递给核心函数并将返回值传递回。您可以在C标准库中看到这种模式,其中包含
printf()
vprintf()
以及类似的配对。

您需要稍微更改方法的签名。我假设你想做这样的事情:

void B(int numParams, va_list parameters)
{
    // Your code here. Example:
    for (int i=0; i<numParams; i++)
    {
        // Do something using va_arg to retrieve your parameters
    }
}

void A(int numParams, ...)
{
    va_list parameters;
    va_start(parameters, numParams);
    B(numParams, parameters);
    va_end(parameters);
}
void B(int numParams,va_列表参数)
{
//此处显示您的代码。示例:
对于(int i=0;i
void B(int numParams, va_list parameters)
{
    // Your code here. Example:
    for (int i=0; i<numParams; i++)
    {
        // Do something using va_arg to retrieve your parameters
    }
}

void A(int numParams, ...)
{
    va_list parameters;
    va_start(parameters, numParams);
    B(numParams, parameters);
    va_end(parameters);
}