Thread: Storing function pointers

  1. #1
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193

    Storing function pointers

    Well.. I'm still trying to develop a kind of event function, I need to store function pointers in a class, so I thought the best way of doing it was with a template. How can I call a function pointer created by a class template? Is there another way of storing function pointers?
    Last edited by lautarox; 09-22-2009 at 05:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'll ask for some extra context, because right now I've got "type the name of the function pointer followed by parentheses", but that's probably not very specific.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, if you're just calling it immediately within a template function you can just invoke it like an ordinary function. If you need to store it somewhere, you'll probably need a store a pointer to some virtual base class, and allocate the derived class to that pointer. Just don't forget to clean up the memory when you're done with it (std::auto_ptr might work for you there, but it has definite limitations).

  4. #4
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    I need a queue, to add function pointers so the functions get executed one by one. What's the best way to store the functions? and How can I execute them when I need?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, this is what I'm seeing:
    Code:
    typedef double (*func_ptr)(double);
    
    std::queue<func_ptr> bob;
    
    bob.push_back(exp1m);
    bob.push_back(sqrt);
    bob.push_back(log);
    
    for (std::queue<func_ptr>::iterator it = bob.start; it != bob.end(); ++it) {
        (*it)(6);
    }
    Obviously I have not run that through a compiler, but I feel pretty comfortable about that.

  6. #6
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    What if I want to store different functions?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by lautarox View Post
    What if I want to store different functions?
    His example (which should have used a deque instead of a queue) does store different functions. It stores exp1m, sqrt, and log.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    But it's storing a double returning function with only an argument.. is it possible to store any kind of functions?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Uh, if it stored any kind of function, how would you use it? You wouldn't know how many parameters to pass, or what type of parameters. That's like asking for a linked list that can store any data type. The question doesn't make sense.
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by lautarox View Post
    What if I want to store different functions?
    See post #3. You can define a non-template base class that provides a virtual function, then a templated derived class that overloads the virtual method to invoke the actual function. Define the function that stores the function pointers in a queue as a template function, and then just do something like this to store it:

    Code:
    queue.push_back( std::auto_ptr< base_t >( new derived_t< Function >( object, function ) ) );
    Or something to that effect.

    EDIT: Nevermind. I thought you had a bunch of function objects.
    Last edited by Sebastiani; 09-22-2009 at 05:43 PM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I feel like this has come up recently: a function pointer must point to a function, and that function has a type that includes the type of its parameters and the return type. As long as all the functions are of the same type, everybody's happy (so as long as all the functions are consistent with each other, not necessarily with my example).

    If you want something you can slap parentheses on and have it do something, that's a function object which requires you to create a class for those function objects to be members of, and will require you to do a little bit of work when you set those functions up (especially if you need to somehow use different numbers of arguments).

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Using different arguments works out to be the same problem with both functions pointers and function objects, namely that you cannot call a function without knowing what arguments to call it with.

    With function objects, this problem can be mitigated by making the object store any extra state it needs. With function pointers, the C approach is to use a single void pointer argument.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. state machine using function pointers
    By meena_selvam in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 02:09 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. function pointers and member functions
    By thor in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2002, 04:22 PM