Jump to content
  • Advertisement
Sign in to follow this  
Alundra

virtual destructor

This topic is 2138 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,

when a class will be inherited a virtual destructor needs to be there but if a class diagram is like that :

IBaseClass1

   -> IBaseClass2
          -> Class1
          -> Class2

IBaseClass1 needs a virtual destructor but IBaseClass2 needs a virtual destructor as well ?

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement

Paradigm Shifter's No is also a yes.  As he said, you don't need to DECLARE a destructor as virtual in derived classes, because once it is defined as virtual in a base, it is virtual from their on down.  So YES, it would be (and need to be) virutal in all derived classes, you just don't have to add the declaration to make it so.

Share this post


Link to post
Share on other sites

You only need a virtual destructor if you cast a pointer/reference to a derived class object to a base class pointer/reference and it could be destructed through that pointer/reference.

Usually that happens if there are other virtual methods in the base class, thats why one should also put a virtual destructor in that case.

Share this post


Link to post
Share on other sites

It still is a good idea to mark the virtual functions as virtual in the derived classes, just so you can instantly see from the header file which functions are actually virtual.

 

Very true.

 

I also like to use 'override', so if changes are made to a virtual function in the base class, you can easily tell which derived classes also need to be modified. They can be easy to miss in large code bases.

 

Not necessary for destructors of course...

class baseClass
{
 virtual SomeFunction();
}

...

class childClass : public baseClass
{
 virtual SomeFunction() override;
}
Edited by Orangeatang

Share this post


Link to post
Share on other sites

Note that override is C++11 only.

 

Probably the most important thing about override is that if you change the function signature in the base class (without changing the signature in the derived class) you will get an error when you compile the derived class (I spent a whole day tracking down that error in someones code once, was called in over the weekend to fix it).

 

However since override is optional rather than mandatory (as it is in C#) it isn't as useful to catch bugs like that as it could be.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

You don't actually need virtual destructor if base class doesn't have any resources (or destructor defined). Omitting virtual destructor from virtual class really confuses developers though.

 

D3DX11 asynchronous loading/processing base classes have virtual functions without virtual destructors, which are meant to be inherited. You need derive them and introduce virtual destructor to make them work correctly.

Share this post


Link to post
Share on other sites

You don't actually need virtual destructor if base class doesn't have any resources (or destructor defined). Omitting virtual destructor from virtual class really confuses developers though.

 

D3DX11 asynchronous loading/processing base classes have virtual functions without virtual destructors, which are meant to be inherited. You need derive them and introduce virtual destructor to make them work correctly.

Wrong conclusion. Those base classes don't need virtual destructors because they aren't freed by calling delete on a pointer to the base class, not because they have no resources in the base class.

Share this post


Link to post
Share on other sites

 

You don't actually need virtual destructor if base class doesn't have any resources (or destructor defined). Omitting virtual destructor from virtual class really confuses developers though.

 

D3DX11 asynchronous loading/processing base classes have virtual functions without virtual destructors, which are meant to be inherited. You need derive them and introduce virtual destructor to make them work correctly.

Wrong conclusion. Those base classes don't need virtual destructors because they aren't freed by calling delete on a pointer to the base class, not because they have no resources in the base class.

 

Yes. But if you're making exception safe engine, you have to call the Destroy() functions automatically.. And for that you have to wrap it with virtual destructor. Sorry I wasn't specific enough in the exaple.

 

EDIT:

Only because documentation says they're COM objects.I remember that some of those wasn't really a COM object. Might remember be wrong. Haven't seen that deprecated crap in years.

Edited by Codarki

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!