Function calls

This is a discussion on Function calls within the C++ Programming forums, part of the General Programming Boards category; Is there a way that I can call a function by having its name stored in a variable? So I ...

  1. #1
    Unregistered
    Guest

    Function calls

    Is there a way that I can call a function by having its name stored in a variable?

    So I can change the value of this variable and a different function will be called with the same line of code. e.g.

    variable = "function1()";

    for (int i = 1; i < 2; i++)
    {
    variable = "function" + i + "()";
    object.variable;
    }

    Am I making any sense and can you see what I am getting at?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You want to postpone deciding what function to call to runtime. You can do that with function pointers, although the syntax is a bit ugly .

    Code:
    #include <iostream>
    
    void funcA() {
    	std::cout << "funcA()\n";
    }
    
    void funcB() {
    	std::cout << "funcB()\n";
    }
    
    int main() {
    	typedef void (*funcPtr)() ;
    	funcPtr ptrToAorB;
    	int response;
    
    	std::cout << "1 for funcA\n2 for funcB\n";
    	std::cin >> response;
    
    	if (response == 1) ptrToAorB=&funcA;
    	else               ptrToAorB=&funcB;
    
    	ptrToAorB();
    	return 0;
    }
    Note the same dynamic resolution of what function to call can be done a bit more elegantly with inheritance and virtual functions.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there a way that I can call a function by having its name stored in a variable?
    Yes, you can do something like what you want with a function pointer:
    Code:
    #include <iostream>
    
    typedef void (*fptr)();
    
    static void function()
    {
      std::cout<<"This is the function\n";
    }
    
    static void function2()
    {
      std::cout<<"This is the other function\n";
    }
    
    int main()
    {
      void (*fp)() = function;
      fp();
      fp = function2;
      fp();
      return 0;
    }
    To match the code you gave, you will need an array of function pointers, like so:
    Code:
    #include <iostream>
    
    void function()
    {
      std::cout<<"This is the function\n";
    }
    
    void function2()
    {
      std::cout<<"This is the other function\n";
    }
    
    int main()
    {
      void (*fp[])() = {
        { function  },
        { function2 }
      };
      for ( int i = 0; i < 2; i++ )
        fp[i]();
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    A post, not long ago, dealt with this concept, albeit from a different angle, and I, in a rare moment of bravado , responded as best I could without really recognizing the benefit or impact.

    SilentStrike and Prelude, both, put it in perspective.

    Thanks to both of you.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accurately detecting function calls
    By Mole42 in forum C Programming
    Replies: 5
    Last Post: 05-17-2009, 04:01 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM

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