Sign in to follow this  
Ignifex

Question about calling functions

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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this