passing a function pointer

This is a discussion on passing a function pointer within the C++ Programming forums, part of the General Programming Boards category; Hello again! I'm working on a project where I need a function to take the address of another function as ...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    passing a function pointer (NEW QUESTION ASKED)

    Hello again!

    I'm working on a project where I need a function to take the address of another function as its argument, then in the function I set a pointer to the address, like here:
    Code:
    virtual void SetFrameFunc( bool &FrameFunc_() ) { pFrameFunc = &FrameFunc_(); }
    This doesn't give me an error, still I don't know how to use pFrameFunc now... Also, I want to do recursion to the function which address was passed to SetFrameFunc, does anyone have any ideas on how to do this?

    Thanks in advance!
    Last edited by Akkernight; 03-21-2009 at 06:25 PM.
    Currently research OpenGL

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You call a function by placing the arguments in parentheses immediately following the name of the function, as
    Code:
    pFrameFunc()
    since your function doesn't appear to take any arguments. I hope you intend the function to return a bool by reference, since that appears to be what you've got.

    I have no idea what you think you mean by "recursion to the function which address was passed to SetFrameFunc". If the function passed in was written recursively, then there's nothing you need to do; if the function wasn't, there's nothing you can do. (Presumably both you and the person who wrote the function agree on what the function is supposed to accomplish; as long as it does so, then you don't care how.) If you mean you want to call it a bunch of times, then by all means call it a bunch of times.

  3. #3
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I want to call it 'til the program shutdowns xP

    EDIT: Also, it still doesn't work... How do I declare the pointer? My current way is...
    Code:
    bool* pFrameFunc;
    Last edited by Akkernight; 03-21-2009 at 04:54 PM.
    Currently research OpenGL

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Akkernight View Post
    I want to call it 'til the program shutdowns xP
    Okay, but that's not recursion, that's a loop.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Doesn't a function pointer look more like?

    Code:
    bool (*FrameFunc)()
    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).

  6. #6
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    http://codepad.org/GQQgZhP0

    There is the code, used codepad 'cause it's easier, now I tried your suggestion anon, still couldn't get it to work :S
    Currently research OpenGL

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, you didn't define pFrameFunc to be a function pointer, hence your attempt to make it one will fail miserably. If you want it to be one, define it as such on line 7.

  8. #8
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I've tried, but then I get invalid use of member message for the pFrameFunc = &FrameFunc thing...
    That also depends on if function pointers are declared like bool* pFrameFunc(); ?
    Currently research OpenGL

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You probably need to look what's before LIQUID.

    But the thing is,

    Code:
    bool* x;
    This is a pointer to a bool.

    Code:
    bool (*x)();
    This should be a pointer to a function that takes no arguments and returns bool.

    Things might be more fishy if what you are trying to pass it is a member function.
    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).

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Again, tried that...
    I guess the problem lies in
    Code:
    virtual void SetFrameFunc( bool &FrameFunc_() ) { pFrameFunc = &FrameFunc_(); }
    Currently research OpenGL

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    //function pointers have a .......... of a syntax, so typedef them
    typedef bool (*FrameFunction)();
    
    class LIQUID {
    private:
    
        SCREEN Screen;
    
        FrameFunction pFrameFunc;
    
    
    public:
    
        LIQUID( bool& failed , const SCREEN Screen_ );
        ~LIQUID();
    
        virtual void SetFrameFunc( FrameFunction FrameFunc_ ) { pFrameFunc = FrameFunc_; }
    
    };
    Something like this.
    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).

  12. #12
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Awesome, it works! Thanks!
    Now, how do I release this pointer? delete says it can't delete functions...
    Currently research OpenGL

  13. #13
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    ok, now, how do I declare the prototype? :S
    Currently research OpenGL

  14. #14
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You don't release function pointers.

    And what prototype do you mean?

    The usage is supposed to be:

    Code:
    bool SomeFunction()
    {
        //...
        return something;
    }
    
    int main()
    {
        //...
        LIQUID liquid(somebool, somescreen);
        liquid.SetFrameFunc(&SomeFunction);
        //...
    }
    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).

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Akkernight View Post
    ok, now, how do I declare the prototype? :S
    The prototype of what? You can't give a prototype of SetFrameFunc, since it's defined right there.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 12-25-2007, 04:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM

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