Thread: make variable1+variable2 into var1var2() func ?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55

    make variable1+variable2 into var1var2() func ?

    hi,

    I was wondering if/is there a way to turn/use 2 char/string variables, join them, and then use the (joined) variable to call a function ?

    For example;
    Code:
    char *varA, *varB; //char variables taken from a table based on user input.
    char *varAB;
    
    //where varAB == varA+varB & subsequently calls different varAB functions
    
    void varAB1(double)
    {
        //do function;
    }
    
    void varAB2(double)
    {
        //do function;
    }
    I've tried using this (& hopefully shows more clearly what I'm 'trying' to do);
    Code:
    #define fPrint1(VAR,double) f ## VAR ## PRINT2(double)
    #include <iostream>
    void fONEPRINT2(double &);
    void fTWOPRINT2(double &);
    
    int main()
    {
      char *table[] = {"ONE", "TWO", "THREE", "FOUR"}; 
      char *TABLE = table[1];
      double i=100;
      fPrint1(TABLE,i);
      std::cin.get();
    }
    
    void fONEPRINT2(double &i)
    {
      std::cout<<"fONEPrint\n";
      std::cout<<i;
    }
    
    void fTWOPRINT2(double &i)
    {
      std::cout<<"fTWOPrint\n";
      std::cout<<i;
    }
    But fPrint1(TABLE,i) always seems to take on the literal meaning TABLE, when I want it to take on its meaning in the *table[] & use that string in order to call the function.

    Just wondering if such a thing is possible, & if so how, or do the functions need to be called by their literal names.


    Any tips/pointers etc appreciated, thanks.
    p.s. still learning.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    you can use strcat() to join two strings - but I'm a little confused as to what you're trying to do - it seems like you want to join two strings together and call a method by that name. It doesn't work so directly - but you could compare the strings to known possibilities and when you find a match, you call that function.

    Pseudocode:

    Code:
    c = a + b;
    if(c == "one") one();
    if(c == "two") two();

  3. #3
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    Thanks for the suggestion.
    Yes, wanting to join two string variables & then call a method using the joined string.
    It's exactly what I am wanting to do, but I would prefer if I didn't have to resort to 100+ switches or if statements, which is why I ask if such a thing is somehow possible.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is also possible to use, for example, an array of function pointers, or a map<string, function pointer>.

    However, normally identifiers don't exit at runtime. You can't concatenate identifiers at runtime and use them.

    If it is not absolutely mandatory to use C++ for this, scripting languages that are aware of all identifiers at runtime make it much easier. E.g in Python:

    Code:
    def foobar():
        print 'Hello world'
    
    if __name__ == '__main__':
        a = raw_input("First part of function name (preferably foo):")
        b = raw_input("Second part of function name (preferably bar):")
        try:
            eval(a + b + "()")
        except:
            print "No such luck"
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  2. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  3. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  4. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  5. Delete & print func()
    By spentdome in forum C Programming
    Replies: 5
    Last Post: 05-29-2002, 09:02 AM