Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Shannon Barber

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

This topic is 6505 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

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!

Share this post


Link to post
Share on other sites
Advertisement
Compiling what you posted resulted in CMyGame::OnConnect being called. I suspect something is happening elsewhere in your code.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • 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!