FAQ 18.03 Why can t subclasses access the private: parts of their base class?

FAQ 18.03 Why can't subclasses access the private: parts of their base class?

The base class encapsulation has to protect itself from being undermined.

Suppose a subclass could access the private: portion of its base class. Would it make sense if just anyone could take away the option for the base class developer to change internal mechanisms in the future because they had subclassed a base class and locked in on its implementation? That would not be in anyone's best interests. So there is a need for ways to distinguish between

  • Mechanisms and services that are available to everyone, the public: interface

  • Mechanisms and services that are available only to subclasses, the protected: interface

  • Mechanisms and services that are reserved for change without concern for breaking user code, the private: interface

Note that this is not a security issue. Developers can always look at the header files to see what is going on. The key notion is that the designer makes different promises to different audiences, and access controls provide a way to do that.

Also, notice that a subclass can access the protected: portion of its base class only when it acts specifically as a subclass. It cannot access the protected: portion of an object of its base class that is freestanding or of a different derived class.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net