Quote:Original post by ToohrVyk
Don't make it virtual. If it absolutely has to be virtual, make it private and have a public function call it.
This is one of the reasons why virtual functions should never be public, only protected: you can make a virtual function private and therefore prevent any overrides.
You can override a virtual function even if it's private.
#include <iostream>class B{ public: virtual ~B() { } void call() { vcall(); } private: virtual void vcall() { std::cout << "B\n"; }};class D : public B{ private: virtual void vcall() { std::cout << "D\n"; }};int main(){ D d; d.call(); return 0;}
Virtual functions shouldn't be public because in my mind at least, "virtual" means "(customisable) implementation detail". Derived classes should be allowed to change the implementation (that's the whole point), but clients shouldn't be able to access it directly.