quick help with non static callback

This is a discussion on quick help with non static callback within the C++ Programming forums, part of the General Programming Boards category; hi im using the following code to demo a callback: how can i change the function _renderCallback to become non ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    quick help with non static callback

    hi im using the following code to demo a callback:
    how can i change the function _renderCallback to become non static?

    thanks.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class MyClass
    {
    public:
    
        MyClass()
    
        {}
    
        ~MyClass() {}
    
        static void _renderCallback(void)
    
        {
            cout << "You clicked." << std::endl;
        }
    };
    
    //------------------------------------------------
    
    
    typedef void(*t_CallBackFunction)() ;
    
    t_CallBackFunction theCallbackFunc ;
    
    
    void callback_registration_function( t_CallBackFunction func )
    
    {
        theCallbackFunc = func ;
    }
    
    void trigger_callback()
    
    {
        theCallbackFunc();
    }
    
    //------------------------------------------------
    
    int main ()
    {
        MyClass obj ;
    
        callback_registration_function( MyClass::_renderCallback ) ;
    
        trigger_callback() ;
    
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't. (Non-static member functions require an object to call them with, which your trigger is not going to have available.)

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    A member function is not an ordinary function, by any means. First of all, it requires an *implicit* pointer to 'this'. Second, the function may well be virtual, in which case the compiler can't simply map it to a physical address at compile time.

    To get that kind of functionality, you're going to need to use a functor (it's a fairly involved idiom, which is why I posted a link instead of an example).

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    What you can do is pass a class pointer instead of a function pointer, then just call a member function of that class. That is the C++ way after all.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    thanks all. im building a new gui and need to know the best way to create a callback to a member function. any ideas other than a functors, it needs to be short and clean.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by bithub View Post
    What you can do is pass a class pointer instead of a function pointer, then just call a member function of that class. That is the C++ way after all.
    how do you pass a class pointer?

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    i think ill create a bass class for all my gui and use a class pointer. thanks all.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    i have a problem. i cant use a class pointer as i need a function pointer.

    my classes need functions like playButtonPressed and volumeControlChanged etc.

    any ideas?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Can the callback store user defined data? What framework are you using?
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by bithub View Post
    Can the callback store user defined data? What framework are you using?
    im using pure c++, no framework to create my gui, im using opengl

    I need a simple way to pass in a pointer to a non static member function.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If you aren't using an existing framework, why can't you use a class pointer instead of a function pointer?
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by bithub View Post
    If you aren't using an existing framework, why can't you use a class pointer instead of a function pointer?
    im a little confused by using a class pointer?

    my gui needs functions like playButtonClicked(bool clicked) and masterVolumeChanged(float level)

    if i use a class pointer? how can i use different functions within my gui

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    So have a linked list of pointers to objects that share a common virtual base class. It's not as clean as the functor approach, but perhaps less complicated. If not that, you could always do things "the C way", eg: static global variables, but then that's going to be even less flexible.

  14. #14
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Whenever you have old C-style callbacks (which are standalone/static, don't have class objects) the equivalent C++-style would be std::function/std::bind (or boost::function/boost::bind). Simply replace these two lines:

    Code:
    typedef void(*t_CallBackFunction)() ;
    Code:
    callback_registration_function( MyClass::_renderCallback ) ;
    with these:

    Code:
    typedef boost::function<void()> t_CallBackFunction ;
    Code:
    callback_registration_function(boost::bind(&MyClass::_renderCallback, obj) ;
    Make _renderCallback non-static, and include <boost/function.hpp> and <boost/bind.hpp> (have to download from boost.org). If you go with this, you should read the documentation.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM

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