Public Group

This topic is 5146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I've got a bit of a question about calling functions, because I'm sure there's a more efficient way of doing the following:
void function1() {
//...
}
void function2() {
//...
}
//...

void callfunction(int i) {
if ( i == 1 ){
function1();
}
else if ( i == 2 ){
function2();
}
}
In AS I could just say like:
this["function"+i]();
Can something like this be done in C++ as well? And how about declaring variables. In AS-syntax:
this[vara+varb] = varc;
If vara would be a string containing: "some" and varb would be a string containing "var", this would set the variable somevar to varc. Thanks in advance. [Edited by - Ignifex on August 18, 2004 5:16:32 AM]

##### Share on other sites
With C++ you could do something like you want however it would be slower then simply doing

void callFunction(int i){   switch(i)   {      case 1:         function1( );         break;      case 2:         function2( );         break;   };}

To do it the way you'd want to do you'd simply use an std::map and function pointers (or functors). I.e something like this

typedef void (*FunctionPointer)(void);std::map<std::string, FunctionPointer> FunctionMap;FunctionMap["function1"] = function1;FunctionMap["function2"] = function2;...std::stringstream ss;ss << i;FunctionMap["function" + ss.str( )]( );

Note that you have to explicity add the functions you want to the map beforehand and that all the functions you add to the function map would have to return void and take no arguments (you can change this by altering the function pointer typedef).

For variables you could do a similar thing only this time the map key would be std::string as the function map but the other value would have to be a class with the = operator overloaded so that it would alter a specific variable using a reference held by the class which you would specify when constructing the class. You could also overload operator int( ) so the class can be implicitly converted to the value it holds e.g. something like this:

class IntBox{private:   int& Ref;public:   IntBox(int& _Ref) : Ref(_Ref) { };   void operator=(int val) {Ref = val;}   operator int( ) {return Ref;}};std::map<std::string, IntBox> IntMap;int SomeVar;IntMap["SomeVar"] = IntBox(SomeVar);...IntMap["SomeVar"] = 20;

Although in C++ you don't really want to be accessing all your functions and variables like this although it can be very useful in some circumstances (e.g. when you want the user at run-time to be able to alter variables by typing varname = value in a console somewhere).

Oh I haven't tested that code, so I may have made a nasty error somewhere [smile]

##### Share on other sites
Nice post and code, Monder. Rate++. [wink]

##### Share on other sites
Thanks, I'll just stick to using the switch statement. I guess that's less coding work.

1. 1
2. 2
Rutin
23
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632579
• Total Posts
3007168

×