简体   繁体   English

C ++无限参数数组

[英]C++ unlimited parameters to array

I have a function with following signature 我有以下签名的功能

char requestApiCall(int num, const wchar_t* pParams = 0, ...)
{
...
}

Now I want to to get all pParams in an array (or to be able to iterate over it). 现在,我想将所有pParams放入一个数组中(或对其进行迭代)。 I know this is possible with some macros, but I have no idea how to do it. 我知道某些宏可以做到这一点,但是我不知道该怎么做。

Any help would be appreciated. 任何帮助,将不胜感激。

PS I'm using MinGW if it matters. 附注:如果有问题,我正在使用MinGW。

UPDATE 更新


my question caused confusion. 我的问题引起了混乱。 I will try to clarify (sorry for my grammar). 我会尽力弄清楚(对不起我的语法)。 Both Object Pascal and C# has the ability to pass unlimited amount of parameters to a method. Object Pascal和C#都可以将无限数量的参数传递给方法。 In C# we achieve this with params keyword: 在C#中,我们使用params关键字实现了这一点:

void Foo(params string[] strs)
{
...
}
Foo("first", "second", "another one", "etc");

I want to achieve same result in C++ without using any object/class. 我想在C ++中实现相同的结果,而不使用任何对象/类。 In my case, type safety is not a concern, but if there is a type safe way to achieve that goal, I will gladly hear your comments :) 就我而言,类型安全不是问题,但是如果有实现该目标的类型安全方法,我很乐意听到您的评论:)

Thanks 谢谢

You need to look at the functions and macros declared in stdarg.h . 您需要查看stdarg.hstdarg.h的函数和宏。 Here is a tutorial that explains it. 这是一个解释它的教程。

http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

I'm not sure what your function parameters are supposed to represent but I think you'll find that it needs to change. 我不确定您的函数参数应该代表什么,但我认为您会发现需要更改它。

By the way, I find that for C++ I can usually avoid variadic functions. 顺便说一句,我发现对于C ++,我通常可以避免可变参数函数。 This has the advantage of preserving type safety. 这具有保持类型安全性的优点。 Are you sure you really need a variadic function? 您确定您确实需要可变参数功能吗?

Using variadic function arguments is a dangerous and tricky business, and almost surely there is a better way - for example, you might pass an std::vector<std::wstring>& to your function! 使用可变参数函数参数是一件危险且棘手的事情,几乎可以肯定有更好的方法-例如,您可以将std::vector<std::wstring>&传递给函数!

OK, that said, here's how to use variadic arguments. 好的,也就是说,这是使用可变参数的方法。 The key point is that it is your responsibility to know the number and types of the arguments ! 关键是知道参数的数量和类型是您的责任

#include <cstdarg>

char requestApiCall(int num, const wchar_t* pParams, ...)
{
   va_list ap;             // the argument pointer
   va_start(ap, pParams);  // initialize it with the right-most named parameter

   /** Perform magic -- YOU have to know how many arguments you are getting! **/

   int a = va_arg(ap, int);      // extract one int
   double d = va_arg(ap, double) // one double
   char * s = va_arg(ap, char*)  // one char*
   /* ... and so forth ... */

   va_end(ap);             // all done, clean up
}

Just for completeness, I would redefine the function as this: 为了完整起见,我将函数重新定义为:

char requestApiCall(std::vector<std::wstring> & params)
{
  for (std::vector<std::wstring>::const_iterator it = params.begin(), end = params.end(); it != end; ++it)
  {
    // do something with *it
  }
  /* ... */
}

A good example of what you are trying to accomplish is the exec family of functions. exec函数家族就是您要完成的一个很好的例子。 exec() takes an variable list of arguments all of which are expected to be const char*. exec()采用可变参数列表,所有参数均应为const char *。 The last item is a NULL ((char*)0). 最后一项是NULL((char *)0)。 The last item is the indicator for when the list of items is complete. 最后一项是项目列表何时完成的指示符。

You can use the variadic macros in stdargs.h as others have described. 您可以像其他人一样在stdargs.h中使用可变参数宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM