This pointer, a derived class, & a method walk into a bar...

Started by
3 comments, last by Shannon Barber 23 years, 4 months ago
Seriously though, I have a question, suppose:
  
class CGameEngine
{
public:
	virtual void OnConnect(HRESULT);

};
class CNetWorkClient
{
	AttachGameEngine(CGameEngine* ge)
	{
	m_pGameEng = ge;
	}
};

class CMyGame : public CGameEngine
{
public:
	virtual void OnConnect(HRESULT);
	StartClient()
	{
	m_pNetClinet->AttachGameEngine(this);
	}

private:
	CNetworkClient* m_pNetClient;
};

  
ok, I traced the StartClient and call and he''s misbehaving as so: StartClient: this = 0x009a46f8 {CMyGame ...} m_pNetClinet->AttachGameEngine(this): this = 0x009a46f8 {CMyGame ...} AttachGameEngine(CGameEngine* ge): ge = 0x009a4758 {CMyGame ...} //Why''d it twiddle the pointer!? Most importantly from the CNetworkClient I call m_pGameEngine->OnConnect(...) and it calls the one defined in CGameEngine, not the overridden on in CMyGame!
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
Compiling what you posted resulted in CMyGame::OnConnect being called. I suspect something is happening elsewhere in your code.
quote:
StartClient: this = 0x009a46f8 {CMyGame ...}
m_pNetClinet->AttachGameEngine(this): this = 0x009a46f8 {CMyGame ...}

AttachGameEngine(CGameEngine* ge): ge = 0x009a4758 {CMyGame ...}
//Why''d it twiddle the pointer!?

Most importantly from the CNetworkClient I call
m_pGameEngine->OnConnect(...) and it calls the one defined in CGameEngine, not the overridden on in CMyGame


Question: Is all of this happening in the constructor of the CNetworkClient object?

The reason I am asking is because the MSDN Library shows this same situation as a caveat to calling virtual functions in the constructors.

Basically, what is happening is that the base class gets created first. Then the child-class. By calling the virtual function within the constructor of the child-class, its virtual function table may not be completely setup yet and therefore runtime binding is not possible so you get the base-class function instead.

They suggest completing the construction and then creating/calling an Initialize() type method to bypass this problem.

Anyway, as I said, the problem you are illustrating seems to fit the MSDN Library description to a "T". Check it out for more details as I may have explained it wrong.

Regards,
Jumpster
Regards,JumpsterSemper Fi
I just compiled an ran what you posted as well and CMyGame::OnConnect was called.

- Dire Wolf
direwolf@digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Yeah, the StartNetwork call is in the CMyGame constructor...
testing it now

ahhhh, the pointers are the same!
It works much better now, thanks!


Edited by - Magmai Kai Holmlor on November 28, 2000 1:31:52 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement