Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

start function with string (like in php)

Status
Not open for further replies.

anchelito

Technical User
Dec 6, 2004
3
0
0
DE
well, that's it: i want to start a function by its name, which is saved in a string as it is possible in php. for example you have an edit-field where you type in the name of the function as a string und click on a button and exactly the function you wrote in will be started. you could also make it with an if-clause, but i want the string to be included as source-code. do you understand what i mean?

here is how it works in php (for example the add-function for an calculator):

(the function is part of the class calc with the properties $n1 and $n2)

function add($n1, $n2)
{
return ($n1 + $n2);
}

$the_function = "add"; // set the_function

$calc->n1 = 2; // set numbers
$calc->n2 = 4;
$result = $calc->$the_function(); // start the function
$print "$result" // 6 is printed

you see: "$calc->$the_function();" is automatically replaced with "$calc->add();". so $result will be 6;

is something like this possible in c++?
thank you for your help.
 
You can't run a function using a string version of it's name, but you can use a pointer to the function.


If you really want to use a string of the function name, you could create a map<> of strings and function pointers and lookup the names in the map...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top