calling the function dynamically

hi,
is there any way to prepare the function call dynamically ?
below is the example for the question
Ex ::
class Example {

int a, char c, double d;
public :
Example () {}
~Example () {}
setA(int a1) { a=a1; }
setB(char c1) { c1=c;)
setB(dobule d1) { d1=d;)
};

int main ()
{
Example e1;
for (i=1;i<=3; i++)
{
e1.//set () // here can i call each setter in each iteration,
// acutal requirement i will have structure of records and i need to set //the value based on each record
}

}


thanks in advance
You can not because your setters have different types. You could do that if the functions would have the same type. In your example the functions have the following types (if to add the return type that you forgot to write )

void ( * )( int );
void ( * )( char );
void ( * )( dobule );

The problem is that inside the loop you use the same type as an argument of functions that is the type of the variable 'i'. So the compiler will not know which function to call if you will pass 'i' as an argument. That is it always will call the same function tha parameter of which is declared as int.
Last edited on
Since the functions have different parameter lists you can not store pointers to all functions in an array or using any other container class i think (but i did not try this yet).

It's not a very smart solution, but maybe this is just fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Example::callFunc(int index, void* param) {
	switch(index) {
		case 1:
			setA(*((int*)param));
			break;
		case 2:
			setB(*((char*)param));
			break;
		case 3:
			setB(*((double*)param));
			break;
		default:
			//Error
			break;
	}
}
Thanks guys for the reply.
Topic archived. No new replies allowed.