Thread: FLTK class method callbacks

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    Exclamation FLTK class method callbacks

    Hello!
    Right now I'm learning FLTK, and using OOP in it.
    I have a class that contains everything, and I want to have a non-static callback (as this lets me use everything in the class). Callbacks have to be static, but it is possible, according to the documentation, to call, from the callback, a non-static method.

    I get the error that it cannot call the non-static method without object.

    So I have tried passing a custom *this using the following method:
    Code:
    application::application()
    {
    //.....
        void* ptt = &*this;
        in->callback(s_enter, ptt);
    //.....
    }//application()
    in here, we are located in the constructor, that can use *this by default.
    So I pass *this as a void pointer. Then...
    Code:
    void application::s_enter(Fl_Widget* o, void* p)
    {
        application* me;
        me = (application*)&p;
        enter(me);
        return;
    }//s_enter()
    I make a pointer to this class object, get *this from the void pointer, essentially getting *this in a static method.
    Then I call the non-static method with the newly hand-made *this.

    I still get the error!:
    Code:
    error: cannot call member function 'void application::enter(application*)' without object
    Please point me to my mistake, because I do not see it, not now at least.

    In case it helps, this is the function prototype for enter:
    Code:
    void enter(application*);
    It never actually uses the pointer though, as I do not needed considering I have access to all of the class' attributes.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you mean "me.enter()" instead of "enter(me)"?

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Yeah, I just tried using:
    Code:
    void application::s_enter(Fl_Widget* o, void* p)
    {
        application* me;
        me = (application*)&p;
        me->enter();
        return;
    }//s_enter
    and it compiled.

    The function is bugged, but I guess that's too bad XD.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Quote Originally Posted by Shingetsu Kurai View Post
    Hello!
    Right now I'm learning FLTK, and using OOP in it.
    I have a class that contains everything, and I want to have a non-static callback (as this lets me use everything in the class). Callbacks have to be static, but it is possible, according to the documentation, to call, from the callback, a non-static method.
    I didn't read the rest of the post very carefully, but I'm assuming it just follows this idea. When I want to do this, just make the static function. Then also make an inline function in the class to call. The static callback will just call this inline and you can do whatever you want in the inline function. Like this -
    Code:
    private:
        static void callback_for_something(Fl_Widget*, void*);
        void callback_for_something_inline();
    
    
    
    ...
    ...
    
    
    void theClass::callback_for_something(Fl_Widget* o, void* v) {
        theClass* w = (theClass*)v;
        w->callback_for_something_inline();
    }   //END CBFULLMODE
    
    
    
    inline void theClass::callback_for_something_inline() {
        //whatever you want to do
    }   //END CBFULLMODE_I
    I think this is a pretty easy approach to it. Hope it helps.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is fraught with so many problems...
    Where is the documentation for this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    this seems like a good application for boost::bind, if they can play nice together.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or lambdas. They play nicer with error messages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    FLTK docs?

    Quote Originally Posted by Elysia View Post
    This is fraught with so many problems...
    Where is the documentation for this?
    You mean for the FLTK?

    FLTK 1.3.0: Modules

    There are better methods than posted above to 'wrap' the implementation, check the docs if wish to view or i will post alternatives later for the op
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It seems from what I see that FLTK only support old C-style callbacks. No boost::bind, no lambdas, no C++ class method callbacks.
    OP: Note that this is a pointer to the current instance. &*this is not necessary.
    Also, in the static callback function, we should use static_cast to get a pointer to the instance back, and there is no need to cast the pointer to void* before passing it in or anything such, just pass this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    c style

    This is true,the developer contributors recently spoke of using tricks and 'old style' coding to keeps things 'fast and light', opinions on this will vary, but these attributes are verfied by the relative size and performance of applications created using FLTK, the drawing is very fast indeed and exe size is kept to a minimum.
    Last edited by rogster001; 08-12-2011 at 08:16 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by rogster001 View Post
    This is true,the developer contributors recently spoke of using tricks and 'old style' coding to keeps things 'fast and light', opinions on this will vary, but these attributes are verfied by the relative size and performance of applications created using FLTK, the drawing is very fast indeed and exe size is kept to a minimum.
    I suspect that using something like boost::bind (std::bind for C++0x/C++11) affects compile performance a lot more than run time performance, so the 'fast and light' argument for using C-style callbacks is probably mostly invalid.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In fact, C++-style callbacks are usually faster than C-style callbacks because they are inline and thus the compiler has a greater chance to optimize the function call.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding method to STL class
    By MK27 in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2010, 04:13 AM
  2. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  3. Puting win32 callbacks into a class
    By Josh Kasten in forum Windows Programming
    Replies: 11
    Last Post: 08-15-2004, 12:48 PM
  4. class method
    By dayknight in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2004, 04:55 PM
  5. callbacks within a class?
    By btq in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2002, 05:51 AM

Tags for this Thread