I have 3 questions regarding the virtual keyword. The code speaks for itself, I have inserted the 3 questions as comments:
#include <iostream> class Base { public: Base() {} void doThing() { this->doThingImpl(); } protected: ~Base() {} private: virtual void doThingImpl() {} }; class Derived : public Base { public: Derived() {} ~Derived() {} // #1 Why does this have to be virtual if it is not to be inherited from? private: virtual void doThingImpl() {} // #2 Why do people make derivded member functions virtual? }; class Derived2 : public Base { public: Derived2() {} virtual ~Derived2() {} private: virtual void doThingImpl() final {} // #3 Being a final member function, should this now be virtual or not? }; int main() { Derived* test = new Derived(); delete test; // warning: deleting object of polymorphic class type 'Derived' which has // non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor] Derived2* test2 = new Derived2(); delete test2; }
1: It doesn't, but there's nothing in the code saying that it can't be inherrited from. The warning protects you from mistakes, but it can have false positives.
2: It is a virtual function, whether marked so or not, and in C++03 you're making that explicit. In C++11 you can mark the function as "overrides" for better documentation of intent and error protection. See also bellow:
3: Final implies virtual, so it is redundant information. However, having virtual there makes it easy to use search functions to find virtual methods. So it comes down to style.