c ++继承可见性模式

Sar*_*nsh 2 c++ inheritance

继承期间类的默认可见性模式是什么(此处为D @ class中的B)

class B {
public:
    int key;
    B(void) { key = 0; printf("B constructed\n");}
    virtual void Tell(void);
    ~B(void) {cout <<"B destroyed"<<endl << endl;}
};


class D2 : B {
public:
    void Tell(void) { printf("D2 Here\n"); }
};
Run Code Online (Sandbox Code Playgroud)

Dan*_*rey 7

因为当你使用默认的classprivate,当你使用默认structpublic.

所以这:

class D2 : B {
Run Code Online (Sandbox Code Playgroud)

相当于

class D2 : private B {
private:
Run Code Online (Sandbox Code Playgroud)

还有这个:

struct D2 : B {
Run Code Online (Sandbox Code Playgroud)

相当于

struct D2 : public B {
public:
Run Code Online (Sandbox Code Playgroud)