Call funtion from a string

This is a discussion on Call funtion from a string within the C++ Programming forums, part of the General Programming Boards category; How i can call function from a char or string class? Such as Code: char handle[] = "actionHandler"; this->handle(); ....

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    Call funtion from a string

    How i can call function from a char or string class?

    Such as
    Code:
    char handle[] = "actionHandler";  this->handle();
    .

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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:
    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;
    }
    Of course, if it's simply function names that come in, then you can use a map as suggested by Laserlight.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    7

    Question

    Quote Originally Posted by matsp View Post
    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:
    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;
    }
    Of course, if it's simply function names that come in, then you can use a map as suggested by Laserlight.

    --
    Mats
    This sample is not a object oriented style code. If you have a 100 or more cases would you use if-elseif-else or switch constructions?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Quote Originally Posted by majesty
    This sample is not a object oriented style code. If you have a 100 or more cases would you use if-elseif-else or switch constructions?
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by majesty View Post
    This sample is not a object oriented style code. If you have a 100 or more cases would you use if-elseif-else or switch constructions?
    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):
    Code:
    class table
    {
       public:
           TestCaseBase *(*creator)(void);
           std::string testName;
    };
    
    table tab[] = 
    {
        { CreateFirstTest, "FirstTest" }, 
        { CreateSecondTest, "SecondTest" }, 
        ...
    };
    
    // Example: 
    TestCaseBase *CreateFirstTest(void)
    {
        return new FirstTest;
    }
    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.

    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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!
    Last edited by m37h0d; 12-29-2008 at 12:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 05-19-2009, 08:17 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21