如何从以下用C / C函数来获取所有的参数++? [英] How to get all arguments from following function in 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
}
函数被调用如下:
function is called as follows:
myMethod(exception, ""Exception message: %s, Exception object %d",
"Hi from Exception", 100);
您可以为 myMethod的在code()
将要访问的所有参数,并打印出来。
Can you provide the code for myMethod()
that will access all the arguments and print them out.
先谢谢了。
推荐答案
在的va_start和va_arg的宏的用于获取可变参数的函数。
一个例子可以在Microsoft网站上找到: HTTP ://msdn.microsoft.com/en-us/library/kb57fad8(v = vs.71)的.aspx
The va_start and va_arg macro's are used to get the variable arguments in a function. An example can be found on the Microsoft site: http://msdn.microsoft.com/en-us/library/kb57fad8(v=vs.71).aspx
在你的情况下,这是一个有点棘手,因为你需要分析格式字符串确切地知道多少个参数应给予和类型的他们。幸运的是,CRT包含一个函数。 vfprintf函数可以给出的va_list(你从va_start中得到)。 vfprintf会用这个来处理所有额外的参数。请参见 http://www.cplusplus.com/reference/clibrary/cstdio/vfprintf/ 为例。
In your case it's a bit trickier, since you need to parse the format string to exactly know how many arguments should be given and of which type they are. Luckily, the CRT contains a function for that. The vfprintf function can be given a va_list (which you get from va_start). vfprintf will use this one to process all the extra arguments. See http://www.cplusplus.com/reference/clibrary/cstdio/vfprintf/ for an example.
这篇关于如何从以下用C / C函数来获取所有的参数++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!