如何传递可变数量的参数从一个函数到另一个? [英] How to pass variable number of arguments from one function to another?

查看:325
本文介绍了如何传递可变数量的参数从一个函数到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法直接从一个函数传递可变数量的参数到另一个?

Is there any way to directly pass a variable number of arguments from one function to another?

我想实现一个最小的解决方案如下: / p>

I'd like to achieve a minimal solution like the following:

int func1(string param1, ...){
  int status = STATUS_1;
  func2(status, param1, ...);
}



我知道我可以使用类似以下的东西,但这段代码将会被重复多次,所以我想保持它尽可能简约,同时保持函数调用非常短

I know I can do this using something like the following, but this code is going to be duplicated multiple times so I'd like to keep it as minimalist as possible while also keeping the function call very short

int func1(string param1, ...){
  int status = STATUS_1;
  va_list args;
  va_start(args, param1);
  func2(status, param1, args);
  va_end(args);
}

谢谢!

推荐答案

不,您必须使用 va_list 按照第二个示例传递varargs。

No, you have to pass the varargs using a va_list as per your second example.

这只是3行额外的代码,如果你想避免重复这些行,func2总是相同,或atleast采用相同的参数,使一个宏出来。

It's just 3 lines extra code, if you want to avoid duplicating those lines, and func2 is always the same, or atleast takes the same parameters, make a macro out of it.

#define CALL_MY_VA_FUNC(func,param) do {\
        va_list args; \
        va_start(args,param);\
        func(param,param,args);\
        va_end(args); } while(0)

这篇关于如何传递可变数量的参数从一个函数到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆