C ++在一个参数中传递多个参数

时间:2012-12-13 20:35:26

标签: c++ function arguments c-preprocessor

这可能听起来令人困惑,但你怎么能在一个ARGUMENT中传递多个参数。

我尝试的是以下内容:

#define CALL(v, look, param, expect) v(param){look(expect);}

使用它时的示例(不起作用):

void CALL(InitD3D, engine->InitD3D, (HWND hWnd, bool windowed), (hWnd, windowed))

// Which should give(i want it to):
// void InitD3D(HWND hWnd, bool windowed){engine->InitD3D(hWnd, windowed);}
// But it may give: InitD3D((HWND hWnd, bool windowed)){engine->InitD3D((hWnd, windowed));}
// Or something similar or not...

基本词也是如此,我如何在一个参数中传递多个参数,而不是搞砸它......

谢谢

2 个答案:

答案 0 :(得分:1)

#define CALL(v, look, param, expect) v param {look expect;}

namespace foo {
  void bob(int x, int y) {}
}
CALL(void bob, foo::bob, (int x, int y), (x,y))

int main() {
  bob(7,2);
}

我会反对这种技巧。

答案 1 :(得分:1)

传递多个参数的最简单方法是使用结构。

示例:

typedef struct{
    int arg1;
    std::string arg2;
    int arg3;
} fn_args;

void bob( fn_args a )
{
   //access them using a.arg1, a.arg2, etc.
}
编辑:你提到过VA_ARGS。当您要传入的参数数量可变时使用。您通常可以解决大多数问题,而无需使用VA_ARGS。

你可能想要看的另一种技术称为Named Parameter Idiom,它更多的是不关心传递函数参数的顺序。因为它不完全清楚你在这里尝试做什么,我给了你一些可能的方法。