How i can call function from a char or string class?
Such as.Code:char handle[] = "actionHandler"; this->handle();
Printable View
How i can call function from a char or string class?
Such as.Code:char handle[] = "actionHandler"; this->handle();
Do you have a better example, more in keeping with what you're trying to achieve?
Where does the string come from (the user?)
How are you going to validate the input?
How many choices are there?
As in given the function name "actionHandler", you want to call the function? One way is to create a map of function names (as strings) to function pointers (or better yet, function objects), then find the corresponding function pointer/object given the function name and invoke it.
We actually have a similar scenario in test-code: A test-script will contain a list of test-cases to execute. Each test-case is it's own class [for 99.5% of the test-code - there are some exceptions where two test-cases share the same class, with for example different constructor parameters - but that is the exception].
The solution, which is not very pretty, but simple, is to simply compare the string coming from the test-script reading code, with the test-case name:
Of course, if it's simply function names that come in, then you can use a map as suggested by Laserlight.Code:TestResult testcase(string casename)
{
TestCaseBase *testCase
TestResult result;
if (casename == "FirstTest") { testCase = new FirstTest; }
else if (casename == "SecondTest") { testCase = new SecondTest; }
else ....
result = testCase->DoTest();
delete testCase;
}
--
Mats
It is object oriented since DoTest() is presumably a virtual member function. The problem is that if you have a number of cases to be created based on some string, then at some point you have to list them all. You could use a map, as I suggested, or you could use an if-else chain or a switch, but somehow you have to have some kind of factory that creates the appropriate object. Once that object exists polymorphism can work as desired.Quote:
Originally Posted by majesty
Unfortunately, I know of no method of storing the which class to construct, so that we can do "new <something>" where something came from a stored location. If you know of such a way to do something, then I'd be happy to suggest that to the folks who came up with the idea. Since each test is it's own class, we need to create the appropriate class.
Yes, of course, we could have a free function that acts as a proxy creator of the class, and we could create a table of (and that table could then be stored as a map, if you prefer that):
It adds another layer of abstraction, which may help in some cases. We'd still need to compare the name [some way or another - using std::map would move it to the inside of std::map] to find the appropriate function, and then call that function, and then call the DoTest() function once the object has been created.Code:class table
{
public:
TestCaseBase *(*creator)(void);
std::string testName;
};
table tab[] =
{
{ CreateFirstTest, "FirstTest" },
{ CreateSecondTest, "SecondTest" },
...
};
// Example:
TestCaseBase *CreateFirstTest(void)
{
return new FirstTest;
}
Whether one or the other solution is better depends mainly on the viewpoint.
[By the way, the actual code is slightoy different from what I posted here - but the principle applies].
--
Mats
assuming i've properly understood what you're after, i'd use a function object to encapsulate the method you want executed then relate the name to the function using std::map.
edit: OOPS laserlight already said that. oh well, if my suggestion matches hers, i must be doing something right! :)