如何从c / c ++中的跟随函数获取所有参数?

以下是我的方法的实现

static VALUE myMethod(VALUE self, VALUE exc, const char* fmt, ...) { // Need to get all the arguments passed to this function and print it } 

函数调用如下:

 myMethod(exception, ""Exception message: %s, Exception object %d", "Hi from Exception", 100); 

你能为myMethod()提供代码来访问所有参数并打印出来。

提前致谢。

va_start和va_arg宏用于获取函数中的变量参数。 可以在Microsoft站点上找到一个示例: http : //msdn.microsoft.com/en-us/library/kb57fad8(v = vs.71).aspx

在你的情况下,它有点棘手,因为你需要解析格式字符串,以准确知道应该给出多少参数以及它们是哪种类型。 幸运的是,CRT包含了一个function。 vfprintf函数可以给出一个va_list(你可以从va_start获得)。 vfprintf将使用这个来处理所有额外的参数。 有关示例,请参见http://www.cplusplus.com/reference/clibrary/cstdio/vfprintf/ 。

一种方法是使用vsnprintf()。

示例代码:

 char buf[256]; va_list args; va_start(args, fmt); if(vsnprintf(buf, sizeof(buf), fmt, args) > 0) fputs(buf, stderr); va_end(args); 

您需要使用va_start和va_arg宏来获取参数。 你可以看看这个 – 它有一些例子。

http://www.go4expert.com/forums/showthread.php?t=17592