View Full Version : C++ question

08-05-2009, 09:49 AM
Hi there,

I have a question:

A function with variable number of arguments:

1) does it have only one prototype declaration?


2) Should it have as many as prototype declaration to be used to actual functions used with different parameters?

08-05-2009, 05:27 PM
You would need to have one declaration for each number of arguments. C++ does not support variable number of arguments directly.

However there are ways around it, if the arguments are the same type, you could put them in a vector and pass that. Then you would have one function prototype and definition and the vector can have as many elements as you want.

08-06-2009, 06:24 AM
So which is the most appropriate answer out of those 2 options?

ralph l mayo
08-07-2009, 05:09 PM
Soon variadic templates (http://en.wikipedia.org/wiki/C%2B%2B0x#Variadic_templates) will be the correct answer.

Right now it's impossible to say what is the most sensible route without knowing more about what you're trying to accomplish.

If your function accepts an arbitrary number of homogeneous arguments, accepting a vector or similar container of them is probably the best.

If you need heterogeneous arguments you might be best off using a functor:

class VarArgs
VarArgs& operator()(const std::string& arg)
return *this;

VarArgs& operator()(const int arg)
return *this;

void operator()() const
std::cout << "Integers: ";
std::copy(ints.begin(), ints.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\nStrings: ";
std::copy(strings.begin(), strings.end(), std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
std::vector<int> ints;
std::vector<std::string> strings;

int main()
VarArgs va;

If a class's operator() returns a reference to the instance it can be chained together. You can store the argument context however it makes sense and then do the actual work in operator()(void).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum