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 inheritance
public:
// 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 n
.
A using
declaration inside a class can name any accessible (e.g., not private
) member of a direct or indirect base class. Access to a name specified in a using
declaration depends on the access specifier preceding the using
declaration. That is, if a using
declaration appears in a private
part of the class, that name is accessible to members and friends only. If the declaration is in a public
section, the name is available to all users of the class. If the declaration is in a protected
section, the name is accessible to the members, friends, and derived classes.