Variable number of parameters in function in C++ -


How can I have a variable number of parameters in my function in C ++?

Analog C #:

  public void Foo (params int [] a) {for (int i = 0; i & lt; a.Length; i + +) Console. WrightLine (a [i]); } Public Zero useFoo () {Foo (); Fu (1); Fu (1, 2); }  

Analog in Java:

  Public Zero Foo (int ... a) {for (int i = 0; i & lt; a. Length; I ++) system. Out.printlean (a [ii]); } Public Zero useFoo () {Foo (); Fu (1); Fu (2); }  

This is called the list of Wikipedia.

In order to be functionally implemented in C programming language, a standard header file should be used. The old varargs.h header has been deprecated in favor of stdarg.h in C ++, the header file cstdarg should be used.

To create a variadic function, an ellipsis ( ... ) should be placed at the end of a parameter list. Within the function, a variable of type va_list should be defined. , va_end (va_list) macros va_start (va_list, last set param) , va_arg (va_list, cast type) can be used. For example:

  #include & lt; Stdarg.h & gt; Double average (integer number, ...) {va_list ap; Int j; Double soft = 0; Va_start (AP, Count); // requires the last fixed parameter (to get the address) (j = 0; j & lt; count; j ++) tot + = va_arg (ap, double); // need to type increase the next argument for AP va_end (AP); Number of return / count; }  

Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -