views:

52

answers:

3

Is it possible to use Input to call a member function?

void one()
{
}

cout << "enter input:" << endl;
cin >> input;  //where input is "one"

instance.input()
A: 

No, you can't - because input is not an lvalue.

Traveling Tech Guy
Then is there anyway to get that accomplished? Else I'm going to have to use a ton of IF statements. If (input == "one") then.... yada yada.
Dakota Irsik
Thank you Patrick.
Dakota Irsik
You should accept Patricks answer if it is good for you (done using the hook on the left of the answer).
Space_C0wb0y
+2  A: 

If all of your methods have the same number of arguments (or preferably, no arguments), then you can easily do this using an std::map.

Use the method name as key of the map, use a pointer to the method as value in the map. Then lookup the entered string in the map, and execute its corresponding method via the method pointer.

Be careful with non-static methods. Function pointers and pointers to static methods are quite easy, but non-static method pointers are a bit more difficult. See http://www.goingware.com/tips/member-pointers.html for more information.

Patrick
I think you might be able to do with with varying numbers of parameters using boost::bind and boost::function too.
Mark B
A: 

Sounds like a job for the Interpreter and the Command pattern.

Larry_Croft