Jump to content
  • Advertisement
Sign in to follow this  
irreversible

subclassing and inheritance

This topic is 2917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I think my flu is getting the best of me. I'm having a very simple problem and some trouble wrapping my head around it. Consider (casting and all such omitted):


typedef void (*callback)(class BaseA *);

class BaseA {
vector<callback> cblist;

HWND handle;

void manage() { cblist[0](this); } //call only the first one for simplicity
void registercallback(callback cb) { cblist.push_back(cb); }
};

class BaseB : public BaseA {
int _int;

BaseB() { _int = 15; registercallback(BaseB::localproc); }

static void localproc(BaseA * a)
{
BaseB* b = (BaseB*)a;
printf(b->_int); //address of BaseB::_int is invalid
}
};

class C : public BaseB {
C()
{
handle = CreateWindow();
SetWindowLong(handle, GWL_WNDPROC, C::WndProc);
SetWindowLong(handle, GWL_USERDATA, this); //<- hic!
}

static LRESULT WndProc(HWND hwnd, UINT iEvent, WPARAM wparam, LPARAM lparam)
{
C* c = (C*)GetWindowLong(hWnd, GWL_USERDATA);
c->manage();
return 0;
}
};





Notably changing the line

SetWindowLong(handle, GWL_USERDATA, this);

produces different results. Eg changing it to

SetWindowLong(handle, GWL_USERDATA, (BaseA*)this);

causes the class' address to not line up when inspecting *this* in BaseB::localproc(). I've never encountered thisbefore . Is this normal?

Normally I don't get WinAPI or C++ brainfarts, but this seems to be one such case. To recap - how do I gain access to BaseB's members in a static member function of BaseB called via a proxy in BaseA and triggered from inherited class C?

Edit: yes, in real life I'm also taking care of WM_CREATE and the window's user data not being set yet.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!