c++ - 将可变参数传递给另一个接受可变参数列表的函数

标签 c++ variadic-functions

所以我有 2 个函数,它们都有相似的参数

void example(int a, int b, ...);
void exampleB(int b, ...);

现在 example 调用 exampleB,但是我如何在不修改 exampleB 的情况下传递变量参数列表中的变量(因为这已经是也用于其他地方)。

最佳答案

你不能直接这样做;您必须创建一个接受 va_list 的函数:

#include <stdarg.h>

static void exampleV(int b, va_list args);

void exampleA(int a, int b, ...)    // Renamed for consistency
{
    va_list args;
    do_something(a);                // Use argument a somehow
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

void exampleB(int b, ...)
{
    va_list args;
    va_start(args, b);
    exampleV(b, args);
    va_end(args);
}

static void exampleV(int b, va_list args)
{
    ...whatever you planned to have exampleB do...
    ...except it calls neither va_start nor va_end...
}

关于c++ - 将可变参数传递给另一个接受可变参数列表的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836169/

相关文章:

c - va_args 解析中的段错误

c++ - 如何在开关条件中使用枚举

c++ - C++读取多个文件

python - 将值从 C++ 发送回 Python

r - 在 R 中的 stats::lm 中使用可变参数(点-点-点)

c - 如何调用参数数量可变的函数?

c# - 在 Linux 上使用 CoreCLR 从 C++ 调用 C# 方法

c++ - 如何使用STM32单片机生成REAL随机数?

java - Arrays.asList() 令人困惑的源代码

c++ - 变量参数列表 : use va_list or address of formal parameter?