DEV Community

Discussion on: Demystifying Virtual and Abstract Functions

Collapse
 
samipietikainen profile image
Sami Pietikäinen

Good introduction to virtual functions! Though there are couple of things I would add. First I would make the base class destructor virtual so the derived class instances are destructed correctly also through base class pointers. Also, in modern C++ (C++11 onwards) it is a good practise to use override keyword in the derived classes. This way the compiler can produce error if the function was not overridden (e.g. missing virtual in base class). It hurts a little inside to see objects not being deleted ;)

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Hm, TIL. I never encountered override until your comment. I'll start using that, and its cousin final. Thanks! :-)

In the meantime, I've edited to use that in the code above.