Is there some way to keep to use overloading with variable number of arguments?
Specific example is as follows:
// Third party class
class ABC
{
public:
void addValue(int);
void addValue(float);
void addValue(string);
void execute(); // Any number of add values can be called before the execute
};
Currently when I want to add values to the object of this class, I have to do as follows: ABC *obj = new ABC(); obj->addValue(2.0); obj->addValue("String"); obj->execute();
Is there some way that in the client code I can do all the addValues in 1 line?
I tried using macros as follow, but then I have to define a macro for every number of arguments:
#define ADD_1_VALUES_TO_CLASS_ABC(obj, val1) { \
obj->addValue(val1) }
#define ADD_2_VALUES_TO_CLASS_ABC(obj, val1, val2) { \
obj->addValue(val1); obj->addValue(val2) }
#define ADD_3_VALUES_TO_CLASS_ABC(obj, val1, val2, val3) { \
obj->addValue(val1) ; obj->addValue(val2); obj->addValue(val3) }
Is there some generic way to define MACRO ADD_N_VALUES_TO_CLASS_ABC and call it like
ABC *obj = new ABC();
MACRO ADD_N_VALUES_TO_CLASS_ABC(obj, "String", 1.0, 4);
MACRO ADD_N_VALUES_TO_CLASS_ABC(obj, 1, 2.0, "String", 4.0, 3);
Also if I use variable number of arguments va_args, I lose the type information needed to call the overloaded function?
Thanks in advance.
ABC& add(int);and then use asobj->add(5).add("String").add(1.0)becomes slightly less cumbersome to write. - David RodrÃguez - dribeas