Thread: function pointer question

  1. #1
    Registered User
    Join Date
    Nov 2005

    function pointer question

    I have wracked my brain and cant seem to think of any way to make a container of function pointers. I was wondering if any of you have a solution. I know using virtual functions is better in C++, but that isnt a option at the moment. I would have to do an entire rewrite to do that.

    Found a solution on another website using std::vector. I am posting a quick example below
    void test()
    	std::cout << "t\n";
    int _tmain(int argc, _TCHAR* argv[])
    	typedef void (*fptr)(void);
    	std::vector<fptr> t;
    	std::vector<fptr>::iterator it;
    	for ( it = t.begin(); it != t.end(); ++it )
    		(*it) = &test; //set function pointer to void test()'s address
    	for ( it = t.begin(); it != t.end(); ++it )
    		(*it)(); //run the function pointed to by t
    	return 0;
    Thank you anyhow, and i hope this helps someone else someday.
    Last edited by Raigne; 02-08-2008 at 11:39 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Perhaps it would make things easier if you made a typedef.
    typedef void (*funcpoint)(int parameter);
    Then you can use "funcpoint" wherever you'd use int or string, e.g.:
    std::vector<funcpoint> v;
    funcpoint array[3] = {f1, f2, f3};
    void function(funcpoint fp);
    You can do all of that without the typedef, too -- but the syntax quickly becomes unwieldy.

    Hopefully that answers your question. See my vector example above.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    A container of function pointers will work, if they are all the same kind of function; viz:
    #include <iostream>
    #include <vector>
    typedef int (*int_ft)(int);
    int bob1(int);
    int bob2(int);
    int bob3(int);
    int main()
        std::vector<int_ft> func_list;
        int j=4;
        std::cout << func_list[1](j);
        return 0;
    int bob2(int x) {
        return x*x;
    int bob1(int x) {
        return x;
    int bob3(int x) {
        return 1-x;
    If your functions are supposed to take wildly different parameter lists, then things are going to get ugly (but I don't obviously see in that case how any kind of container notation would work).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. Function pointer question
    By sbayeta in forum C Programming
    Replies: 9
    Last Post: 08-06-2004, 08:15 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM