Default inheritance access specifier

前端 未结 9 1703
北海茫月
北海茫月 2020-12-04 19:12

If I have for example two classes A and B, such that class B inherits A as follows:

class B: public A

9条回答
  •  春和景丽
    2020-12-04 19:16

    The "type" of inheritance depends on how the class is defined. There are default access specifiers applied to inheritance. From the C++ standard:

    [class.access.base]/2

    In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class. [ Example:

    class B { /* ... */ };
    class D1 : private B { /* ... */ };
    class D2 : public B { /* ... */ };
    class D3 : B { /* ... */ };             // B private by default
    struct D4 : public B { /* ... */ };
    struct D5 : private B { /* ... */ };
    struct D6 : B { /* ... */ };            // B public by default
    class D7 : protected B { /* ... */ };
    struct D8 : protected B { /* ... */ };
    

    Here B is a public base of D2, D4, and D6, a private base of D1, D3, and D5, and a protected base of D7 and D8.  — end example ]

提交回复
热议问题