callback in a class, what do I do?

This is a discussion on callback in a class, what do I do? within the C++ Programming forums, part of the General Programming Boards category; I have a window callback in a class, and I want to access another varible in that class from the ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691

    callback in a class, what do I do?

    I have a window callback in a class, and I want to access another varible in that class from the callback, but when I put the name of the varible down in there, it doesn't recognize it. So how do you access another member of a class from a callback member?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    Your callback function is static, isn't it? In that case you can only access static members.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Pass a pointer to the object to the static callback, then cast that to the object and call a member function from it.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    But it's a callback, I only need to access other members when all the arguments are taken.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, if I get it right, you are saying that the callback has no "spare" or "user defined" parameters that you could use to pass for example a pointer to your object?

    Can you perhaps tie the object to some parameter that is known (and unique) in the callback - for example, if you have a "HWND" that is a handle to your particular window, you could use that in to find your object back again, by having a structure like this:

    Code:
    struct hwnd_to_object
    {
        HWND hwnd;
        CObject *pObject;
    } hwnd_list[10];
    
    ...
       i = find_next_free(hwnd_list);
       hwnd_list[i].hwnd = hwnd;
       hwnd_list[i].pObject = myObject;
    ...
    I'm not saying you have to use the HWND, but anything that is unique that is known at bot ends of the call is what you need.

    --
    Mats

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    Hmm, good idea. Thanks.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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