Despite all the calls to declare the virtual member private, the argument simply does not contain water. Often, overriding a derived class of a virtual function must invoke a version of the base class. It cannot if private declared:
class Base { private: int m_data; virtual void cleanup() { } protected: Base(int idata): m_data (idata) {} public: int data() const { return m_data; } void set_data (int ndata) { m_data = ndata; cleanup(); } }; class Derived: public Base { private: void cleanup() override {
You must declare the base class method protected and indicate that the method should be overridden, but not called.
class Base { ... protected:
So Herb Sutter guideline # 3 ... But the horse still gets out of the barn.
When you declare something protected , you implicitly trust the writer of any derived class to understand and correctly use the protected internal components, as a way of declaring friend implies a deeper trust for private members.
Users who misbehave violate this trust (for example, are marked as "ignorant" without bothering to read your documentation) themselves are to blame.
Update . I had some feedback stating that you can βchainβ the implementation of virtual functions in this way using private virtual functions. If so, I would like to see it.
The C ++ compilers I use will definitely not allow a derived class implementation to invoke a private base class implementation.
If the C ++ committee relaxed "private" to allow this particular access, I would be all for private virtual functions. In its current form, we are still advised to lock the barn door after the horse has been stolen.
Spencer Feb 25 '16 at 16:00 2016-02-25 16:00
source share