strings to function

This is a discussion on strings to function within the C++ Programming forums, part of the General Programming Boards category; Hi i am wondering if it is possible to get a string to be converted into a function such as ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    strings to function

    Hi i am wondering if it is possible to get a string to be converted into a function such as below:
    Code:
    function ("Update");
    
    void function(string FunctionToCall)
    {
    **Do something**
     pTr-> FunctionToCall();
    };
    the only other solution i could thing of is by doing a large switch statement and call the appropriate one by name

    Thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    You could use a std::map to map strings to function pointers.
    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

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by dudecooper123 View Post
    Hi i am wondering if it is possible to get a string to be converted into a function such as below:
    Code:
    function ("Update");
    
    void function(string FunctionToCall)
    {
    **Do something**
     pTr-> FunctionToCall();
    };
    the only other solution i could thing of is by doing a large switch statement and call the appropriate one by name

    Thank you
    The only way you can directly convert a string to a function is to export the function, then use platform specific methods of getting the exports (LoadLibrary, GetProcAddress on Windows).

    Another option is to have a map of strings to function pointers. This only works if all the functions return the same thing, and take the same parameters.

    A switch statement (like you said) won't work though since you can't use strings in a switch statement. You would need if/else statements instead.

    If you go into some more detail on what you are trying to accomplish, we can help you come up with the best solution to your problem.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    i am trying to write a messaging system that could pass a string to say what function needs to be called. Typically i would like it to say

    Code:
    function("Car","StartEngine")
    
    function(string type, string function)
    {
       (type*)Ptr->function;
    }
    an example could be a driver turns the car on but the driver doesnt have a pointer to the car so a messaging system could send a message to the car to say turn on.

    thank you.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by dudecooper123 View Post
    i am trying to write a messaging system that could pass a string to say what function needs to be called. Typically i would like it to say

    Code:
    function("Car","StartEngine")
    
    function(string type, string function)
    {
       (type*)Ptr->function;
    }
    an example could be a driver turns the car on but the driver doesnt have a pointer to the car so a messaging system could send a message to the car to say turn on.

    thank you.
    Why don't you pass a pointer to a function in the first place? Might not be possible for you, but it usually is.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    I think to use that method the driver would have to have access to the car to get the function. so i dont think that would work. i will have a go at this later though.

    Thank you

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Does it have to be done in C++? There are far better languages out there for this sort of thing.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    yeah really. Its going to be used for a game and the rest of the code is done within c++. Its a shame c++ is not more like c#

  9. #9
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    string func1 ()
    {
           string name;
           getline (cin,name);
           cout << name << endl;
           return name;
    }
    
    
    
    
    string func2 (string x);
    
    
    
    ////////////////////// main here we start//
    int main ()                              //
    {                                        //
        string name_in_main;                 //
                                             //
        name_in_main = func1 ();             //
                                             //
                                             //
                                             //
        func2 (name_in_main);                //
                                             //
        return 0;                            //
    }                                        //
    ///////////////////////////////////////////
    
    
    
    
    string func2 (string nameq)
    {
          cout << nameq << endl;
          getline (cin,nameq);
          cout << nameq << endl;
    }

    I have just written for you

    copy and paste in your compiler

    and tell me if this is what you were looking for


    well here you can use more than 1 word as I used
    Code:
    getline (cin,"string")
    instead of
    Code:
    cin >> "string";
    you can use
    Code:
    cin >> "string";
    too if you wish

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    no sorry, something like this would be good where nothing need to know about anything else but the message system,
    Code:
    class Car()
    {
    	void StartCar()
    	{
    
    	}
    }
    
    
    class Driver()
    {
    	void TurnKey()
    	{
    		MsgSystem->sendMessage("Car","StartCar");
    	}
    }
    
    
    class MsgSystem()
    {
    
    	int * Pointer = NULL;
    
    	void sendMessage(string Type, string Function)
    	{
    		(type*)Pointer->Function; 
    	}
    }
    i think i going to try look at how windows messaging works

    Thanks

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by military genius View Post
    some code
    I don't think you understand what the OP is asking for.

    Quote Originally Posted by military genius View Post
    instead of
    Code:
    cin >> "string";
    you can use
    Code:
    cin >> "string";
    too if you wish
    What?
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dudecooper123 View Post
    i think i going to try look at how windows messaging works

    Thanks
    Well, we wish you the best of luck.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    8
    cheers for your help guys, hope i get it sorted

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by dudecooper123 View Post
    yeah really. Its going to be used for a game and the rest of the code is done within c++. Its a shame c++ is not more like c#
    I am curious. In what regard is C# better in this aspect?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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