Parent class has a Virtual function. Is it necessa

2019-06-08 12:26发布

问题:

Parent class has a Virtual function. Is it necessary to have a virtual destructor in the parent class ?

So the questions are 1. Now considering rule of three , should we declare the other two?

回答1:

A virtual destructor is required any time delete is called on a pointer to that class where the object being deleted is actually of a more derived type. If your base class may be used in this sort of situation then you must provide a virtual destructor.

It is usually advisable to add a virtual destructor to any class that has virtual functions as this gives safety and flexibility to future users of the class and the additional cost of adding a virtual destructor to an already polymorphic class is typically low.

This last rule is just a rule of thumb, though. It may be necessary to have a virtual destructor in a class even if it doesn't have any other virtual functions and, conversely, a virtual destructor may not be needed for a polymorphic class if it is never used in a context that requires this (typically the destructor would be made protected or even private to enforce this).

You only need to provide a user-defined copy constructor and a copy assignment operator if the compiler provided default implementations won't do the correct thing. If you've added a destructor with an empty implementation purely to make it virtual, this is unlikely to have any bearing on the need for you to provide a copy constructor and a copy assignment operator.



回答2:

The only real reason to have a virtual destructor in your class is if your ever plan to delete objects of that class polymorphically, i.e. if you plan to delete objects of derived class through pointers to base class. If never do that, you don't need virtual destructor.

However, if you already have a virtual function in your class, then making the destructor virtual bears no appreciable overhead (since the VMT pointer has already been introduced into your object). Under such circumstances it might be a good idea to make the destructor virtual. Just in case.



回答3:

It makes sense for a class with a virtual function to have a virtual destructor, since it is a class which is designed to be inherited from and likely used polymorphically (see related SO question). And if it is used polymorphically, in the sense that a pointer to the parent class points to an instance of a derived type, then the parent class must have a virtual destructor.

On the other hand, it would only be necessary to follow the rule of three in the parent if the parent's class destructor actually did something which a compiler-synthesized one wouldn't do (e.g. explicitly dealing with the release of some resources). If the parent's destructor is trivial and only declared to allow for correct destruction of polymorphic objects from a parent class pointer, there is no need to provide its copy constructor and copy assignment operator.



回答4:

Technically you MUST have a virtual destructor only if you are going to polymorphically delete instances. In other words you can have virtual methods without having a virtual destructor.

From a practical point of view however there is rarely a reason for you to not declare the destructor virtual and it should be done for any class that you think could be used as a base.

The rule of three is talking about program logic: if you have logic in any of destructor, copy constructor or assignment then you probably need logic in all three of them. Note however that it's not a strict rule from a formal point of view and there are rare cases in which it simply doesn't hold (for example I may be interested in seeing a line in a log for every object created, but not on assignment or destruction).

Note also that in my experience when you have inheritance and polymorphism then the copy semantic and even static typing of C++ sometimes won't feel right.



回答5:

Any class that could be a base class a some point should have it's destructor declared virtual. Independent of wheter another member function is virtual or not.

The rule of three says that you should declare a

  • destructor
  • copy constructor
  • copy assignment operator

if you declare one of them. Independantly of whether the destruction is virtual.



标签: c++ oop