A derived class can call a protected base class constructor in its ctor-initializer, but only for its own base class subobject, and not elsewhere:
class Base {
protected:
Base() {}
};
class Derived : Base {
Base b;
public:
Derived(): Base(), // OK
b() { // error
Base b2; // error
}
};
What does the standard say about this? Here is [class.protected]/1:
An additional access check beyond those described earlier in Clause 11 is applied when a non-static data member or non-static member function is a protected member of its naming class (11.2) As described earlier, access to a protected member is granted because the reference occurs in a friend or member of some class
C
. If the access is to form a pointer to member (5.3.1), the nested-name-specifier shall denoteC
or a class derived fromC
. All other accesses involve a (possibly implicit) object expression (5.2.5). In this case, the class of the object expression shall beC
or a class derived fromC
. [ Example: ...
Is there an object expression involved when calling a constructor? There isn't, is there? So where in the standard is access control for protected base class constructors described?
See Question&Answers more detail:os