【问题标题】:Passing variadic parameters from one function to another [duplicate]将可变参数从一个函数传递到另一个[重复]
【发布时间】:2020-05-03 02:01:18
【问题描述】:

我正在调用一个函数 firstFunction 并将其传递给可变参数 (const char* fmt, ...) :我想在这里执行一些计算,然后将参数传递给另一个函数 secondFunction,它以 (const char* fmt, ...) 作为输入参数。

void firstFunction(const char* fmt, ...)
{
 //does some pre-condition stuff

//calls secondFunction (how?)
}

void secondFunction(const char* fmt, ...)
{
//prints the input parameters in a va_list using vsnprtinf
}     

将可变参数从一个函数传递给另一个函数的 C++ 语法是什么?

编辑 1:我不能使用模板,因为我希望这些函数是纯虚函数声明的定义。

【问题讨论】:

标签: c++ parameters parameter-passing variadic-functions


【解决方案1】:

这是一个可变参数,而不是参数包。在 c++ 中你会写:

    template <class... Args>
    void firstFunction(const char* fmt, Args&&... a) {
        secondFunction(fmt, std::forward<Args>(a)...);
    }

【讨论】:

  • 你是对的,问题已更正
猜你喜欢
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 2013-03-28
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
相关资源
最近更新 更多