Whilst I think it's probably not a good design to use ... [because it tends to cause "bad code" and "subtle errors"], the following code is something I just hacked up to show the mechanisms of variable arguments:
Code:
#include <iostream>
#include <cstdlib>
#include <cstdarg>
using namespace std;
class Node
{
private:
int n;
public:
Node(int x) : n(x) {};
void printMe() const { cout << n << endl; };
};
class A {
public:
void DoSomething(int argCount, ...);
};
void A::DoSomething(int argCount, ...)
{
va_list v;
Node *node;
va_start(v, argCount);
for(int i = 0; i < argCount; i++)
{
node = va_arg(v, Node *);
node->printMe();
delete node;
}
va_end(v);
}
int main( )
{
A a;
a.DoSomething(3, new Node(1), new Node(2), new Node(3));
return 0;
}
Note that you have to have at least one parameter that is names. If you don't want to use a count, you could have a "marker" at the end, such as a "NULL" or some such.
--
Mats