4 ms·
Pure virtual destructors in C++
- android2 16y ago"To work correctly, classes with virtual methods must also have virtual destructors." Not a good way to start the article... I immediately question the author's credibility following this statement.
- tomjen3 16y agoWhy? If they don't it is going to mess up the destruction sequence if they are ever stored and then released polymorphically, such as from a collection of pointers to classes. What he is saying is standard C++ knowledge.
- meastham 16y agoI didn't know you could define member functions that were declared pure virtual. The more you know..
- jimwise 16y agoAll of which is a workaround for the fact that there is no explicit way to declare a c++ class to be abstract-only. What happens if you do have shared logic to put in the abstract base class's destructor? It can't be pure virtual, and you're back to being able to instantiate your 'abstract' base class...
- _stephan 16y agoWhat about making the constructors protected?
- thought_alarm 16y ago> All of which is a workaround for the fact that there is no explicit way to declare a c++ class to be abstract-only. Sure you can. Just put the word "Abstract" in the class name, and then every programmer who reads that class name will know that it's abstract. What more does one need? That approach is much clearer than resorting to tricks like a pure virtual destructor. At the end of the day it's all about communicating design.
- joelburget 16y agoIt's exactly this kind of thing that lead me away from C++. There are far too many gotcha's and tricks you need to know to write good C++.