How I can have variable number of parameters in my function in C++.
Analog in C#:
public void Foo(params int[] a) {
for (int i = 0; i < a.Leng
These are called Variadic functions. Wikipedia lists example code for C++.
To portably implement variadic functions in the C programming language, the standard stdarg.h header file should be used. The older varargs.h header has been deprecated in favor of stdarg.h. In C++, the header file
cstdargshould be used.To create a variadic function, an ellipsis (
...) must be placed at the end of a parameter list. Inside the body of the function, a variable of typeva_listmust be defined. Then the macrosva_start(va_list, last fixed param),va_arg(va_list, cast type),va_end(va_list)can be used. For example:
#include
double average(int count, ...)
{
va_list ap;
int j;
double tot = 0;
va_start(ap, count); //Requires the last fixed parameter (to get the address)
for(j=0; j