Jump to content
  • Advertisement
Sign in to follow this  
moussen15

Calling Pure virtual methods from destructor?

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

why can't I do this?: class myInterface { public: virtual ~myInterface(){doYourThing();} virtual void doYourThing() = 0; }; It should be possible seeing as all the instances (ie subclass-instances) of this interface should have been required to supply an implementation for doYourThing. Thus there is absolutely no risk that it might still be virtual when the call is made. Yet I get this error message: myfilename.cpp: In destructor 'virtual myInterface::~myInterface(): myfilename.cpp:6: error: abstract virtual 'virtual void myInterface::doYourThing()' I am using g++. Thank you for your enlightening me :)

Share this post


Link to post
Share on other sites
Advertisement
The derived classes will have been destroyed by the time the function is called, so it just resolves to the classes own implementation. [smile]

Share this post


Link to post
Share on other sites
When a base class' destructor is called, the destructors for all derived classes have been called. You can think of it like this: at that point the type of the class is no longer the type of the derived type, it's the type of the base class, and the base class doesn't implement doYourThing() so you have a problem.

The reason for this behavior is that because the destructors for all the derived classes have been called, the member variables that the derived classes defined have also heen destroyed, so a virtual member function of the derived class should not be called since it may depend on a variable that has been destroyed.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!