Virtual Constructors and Destructors

virtual is never used for constructors, but it is always a good idea for destructors. Otherwise, the superclass destructor may be called when a derived class is being destroyed through a superclass pointer or reference (by delete, in other words), and any storage allocated from the heap (or other resources claimed by the derived class) may not be disposed of properly. The fact that aTape's destructor is virtual ensures that its descendants will be disposed of safely. Since “virtuality” is inherited, aPersistentTape has a virtual destructor courtesy of aTape.

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.