Default inheritance access specifier

前端 未结 9 1676
北海茫月
北海茫月 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 ]

    0 讨论(0)
  • 2020-12-04 19:23

    AS other casting problem you have

    class A { virtual void test() = 0; };
    class B : virtual public A { virtual void testb() {} };
    class C : virtual public A { virtual void testc() {} };
    class D : public B, public C {
     virtual void test() override {}
    }
    
    void main() {
       D d;
       void* v = &d;
       A* a = &d;
    
       ((D*)A)->test(); //OK
       ((D*)v)->test(); //undefined behavior (that call testb() in vtable logic at 1st inheritance position)
       dynamic_cast<D*>(v)->test(); //compile error cast from void* not permitted
    
       //resolution
       void* x = a;
       ((D*)x)->test(); //OK but as you can see, you must to store a* in x*
    
    }
    
    0 讨论(0)
  • 2020-12-04 19:28

    If you use class to define your class, the default access specifier will be private. (I think it's wrong, too.) If you use struct, however, it will be public.

    And class definitions are declarations, I think. A statement is what translates into actual code (unless optimized away, anyway).
    However, a mildly exotic feature of C and C++ is that expressions are statements. That's why 3+4; is a syntactically legal statement in C++ (although many compilers will warn about it having no effect). While it is obviously nonsense in this case, in general expressions are evaluated for their side effects. (An obvious example is discarding a function's return value. You call the function not to obtain a result, but for its side effects.)

    0 讨论(0)
  • 2020-12-04 19:34

    the default access specifier is an important differentiator between classes and structs. It is public by default for structs and private by default for classes.

    0 讨论(0)
  • 2020-12-04 19:37

    If you do not choose an inheritance, C++ defaults to private inheritance in the same way class members default to private access for classes.

    0 讨论(0)
  • 2020-12-04 19:38

    It's private for class and public for struct.

    Side answer: No, these are definitions of the class according to the standard. Class definition end with a semicolon. On the other hand not all statements end with a semicolon (e.g. an if statement does not).

    0 讨论(0)
提交回复
热议问题