#### Archived

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

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

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

## 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 on other sites
Compiling what you posted resulted in CMyGame::OnConnect being called. I suspect something is happening elsewhere in your code.

##### 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 on other sites
I just compiled an ran what you posted as well and CMyGame::OnConnect was called.

- Dire Wolf
direwolf@digitalfiends.com

##### 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

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633724
• Total Posts
3013556
×