Access to protected constructor of base class

后端 未结 2 710
滥情空心
滥情空心 2020-12-19 00:26

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:

c         


        
2条回答
  •  伪装坚强ぢ
    2020-12-19 01:29

    C++11 §11.2/5:


    A member m is accessible at the point R when named in class N if

    • m as a member of N is public, or

    • m as a member of N is private, and R occurs in a member or friend of class N, or

    • m as a member of N is protected, and R occurs in a member or friend of class N, or in a member or friend of a class P derived from N, where m as a member of P is public, private, or protected, or

    • there exists a base class B of N that is accessible at R, and m is accessible at R when named in class B.

    For your constructor invocation

    Base b2;
    

    the 3rd point above applies. m is the Base constructor. N, the naming class, is Base. m as a member of Base is protected, and the declaration occurs in a member of class Derived derived from Base, but it's not the case that the Base constructor as a member of Derived is public, private or protected: it’s simply not a member of Derived, constructors are not implicitly inherited.

    I think the language “is public, private, or protected” is pretty awkward; I can only surmise that it’s the result of some evolution of this paragraph.

    I have yet to find an explanation of how formally the protected Base constructor is accessible in a member initializer list in Derived, but then I just started looking at this for this question.


    Update: I fail to find any language in the standard pertaining to access to constructors in an initializer list, and I fail to find any Defect Report about it. It’s quite possibly a defect.

提交回复
热议问题