Jump to content
  • Advertisement
Sign in to follow this  
Xaeron

Passing a pointer to a derived class pointer

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

Hi Guys, My mind has gone blank and I'm having a daft problem. I have a pointer: cControl *m_Controller; which i'm setting to a derived class: m_Controller = new cPlayerControl(); Elsewhere I am then trying to set another m_Controller pointer in a class to the m_Controller above, with: void Object::SetController (cControl *pController) { m_Controller = pController; } I am calling this function like this: m_Object[0].SetController(m_Controller); if I then call Update() on m_Controller in the same function, Update() will work and run. But if I try and call m_Controller->Update() from within anywhere else in the Object class, simply nothing happens, the function is not run. I'm assuming the pointers are getting messed up somewhere as Im setting a pointer to a derived class pointer? Any help would be much appreciated, as I know it's something daft I'm just having one of those days! ;) Cheers!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Xaeron
if I then call Update() on m_Controller in the same function, Update() will work and run.

But if I try and call m_Controller->Update() from within anywhere else in the Object class, simply nothing happens, the function is not run.

I'm assuming the pointers are getting messed up somewhere as Im setting a pointer to a derived class pointer?


Any help would be much appreciated, as I know it's something daft I'm just having one of those days! ;)

Cheers!
What does the debugger tell you? You can trace into the function to find out what function is being called. Is your Update() function virtual? If it's not, then the function called will depend on the type of the pointer.

Share this post


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