Search code examples
c++virtual-destructor

Is there any harm having a virtual destructor without a virtual method?


During my design sometimes I have the situation that I add / or remove virtual methods. The rule of thumb I know is, that I shall have a virtual destructor once I have virtual methods.

My question: Is there any harm if I do add a virtual destructor straight away when I create the class (so even with no virtual methods yet)? Basically the idea is not to forget it later. Especially with n derived classes I would not need to change it in n places later.


Solution

  • There's a tiny overhead in the size of the virtual function table. Probably not worth worrying about. A virtual destructor will also make your class a non-aggregate, a non-trivial class, a non-standard-layout class, and therefore also a non-POD class. This may be undesirable, depending on the problem at hand.

    However, I recommend specifically designing your classes to be either polymorphic or not. If they're going to be used polymorphically, give them a virtual destructor. If not, don't. If you ever need to change it, do it when you need it.