Exempting Individual Members
Sometimes we need to change the access level of a name that a derived class inherits. We can do so by providing a using
declaration (§3.1, p. 82):
class Base {public: std::size_t size() const { return n; }protected: std::size_t n;};class Derived : private Base { // note: private inheritancepublic: // maintain access levels for members related to the size of the object using Base::size;protected: using Base::n;};
Because Derived
uses private
inheritance, the inherited members, size
and n
, are (by default) private
members of Derived
. The using
declarations adjust the accessibility of these members. Users of Derived
can access the size
member, and classes subsequently derived from Derived
can access ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.