protected inheritance

Accessibility of Derived-to-Base conversion in protected inheritance.
这是一个粑粑一样的feature。

class B;
class C;
class D;
class A{
    public:
        virtual ~A(){}
        friend void gg(D* d);
        void ga(B *b,C* c,D* d); 
        int mem;
    };

class B:protected A{
    public:
        void gb(B *b,C* c,D* d);
};
class C:protected B{
       public:
        void gc(B *b,C* c,D* d);
};
class D:protected C{
       public:
        void gd(B *b,C* c,D* d);
};

void A::ga(B *b,C*c,D*d){
    int tmpa0 = b->mem; // illegal
    int tmpa1 = c->mem; // illegal
    int tmpa2 = d->mem; // illegal
    A *a1=b;  //  'A' is an inaccessible base of 'B' 
    A *a2=c;  //  'A' is an inaccessible base of 'C' 
    A *a3=d;  //  'A' is an inaccessible base of 'D' 
}
void B::gb(B *b,C*c,D*d){
    int tmpb1 = b->mem;
    int tmpb2 = c->mem; // illegal
    int tmpb3 = d->mem; // illegal
    A *a1=b;  // no problem here
    A *a2=c;  // illegal
    A *a3=d;  // illegal
}
void C::gc(B *b,C*c,D*d){
    int tmpc1 = b->mem; // illegal
    int tmpc2 = c->mem;  // no problem here
    int tmpc3 = d->mem; // illegal
    A *a1=b;  // no problem here
    A *a2=c;  // no problem here
    A *a3=d;  // illegal
}
void D::gd(B *b,C*c,D*d){
    int tmpd1 = b->mem; // illegal
    int tmpd2 = c->mem;  // illegal
    int tmpd3 = d->mem;  // illegal
    A *a1=b;    // no problem here
    A *a2=c;    // no problem here
    A *a3=d;    // no problem here
}
void gg(D* d){
    A* a=d;  // illegal
}
int main(){
    B b;
    C c;
    D d;
    A a;
    gg(&d);  //  'A' is an inaccessible base of 'D'
    a.ga(&b,&c,&d);
    b.gb(&b,&c,&d);
    A a1(d);  // illegal
    A a4=d;  // illegal
    return 0;
}

继承树全部用protected,基类无法访问派生类对象,如B中无法访问C中的mem。
如果派生类能够进行隐式转换,即Derived-to-Base conversion,那么它的直接基类和间接基类直到root,都可以进行这个转换。


class B;
class C;
class D;
class A{
    public:
        virtual ~A(){}
        friend void gg(D* d);
        void ga(B *b,C* c,D* d); 
        int mem;
    };

class B:protected A{
    public:
        void gb(B *b,C* c,D* d);
};
class C:public B{
       public:
        void gc(B *b,C* c,D* d);
};
class D:protected C{
       public:
        void gd(B *b,C* c,D* d);
};

void A::ga(B *b,C*c,D*d){
    int tmpa0 = b->mem; // illegal
    int tmpa1 = c->mem; // illegal
    int tmpa2 = d->mem; // illegal
    A *a1=b;  //  'A' is an inaccessible base of 'B' 
    A *a2=c;  //  'A' is an inaccessible base of 'C' 
    A *a3=d;  //  'A' is an inaccessible base of 'D' 
}
void B::gb(B *b,C*c,D*d){
    int tmpb1 = b->mem;
    int tmpb2 = c->mem; // no problem
    int tmpb3 = d->mem; // illegal
    A *a1=b;  // no problem here
    A *a2=c;  // no problem here
    A *a3=d;  // illegal
}
void C::gc(B *b,C*c,D*d){
    int tmpc1 = b->mem; // illegal
    int tmpc2 = c->mem;  // no problem here
    int tmpc3 = d->mem; // illegal
    A *a1=b;  // no problem here
    A *a2=c;  // no problem here
    A *a3=d;  // illegal
}
void D::gd(B *b,C*c,D*d){
    int tmpd1 = b->mem; // illegal
    int tmpd2 = c->mem;  // illegal
    int tmpd3 = d->mem;  // no problem here
    A *a1=b;    // no problem here
    A *a2=c;    // no problem here
    A *a3=d;    // no problem here
}
void gg(D* d){
    A* a=d;  // illegal
}
int main(){
    B b;
    C c;
    D d;
    A a;
    gg(&d);  //  'A' is an inaccessible base of 'D'
    a.ga(&b,&c,&d);
    b.gb(&b,&c,&d);
    A a1(d);  // illegal
    A a4=d;  // illegal
    return 0;
}

修改C:protected B 为C: public B.
在B中可以访问C中的mem,即gb函数中,c->mem是可一访问的,于是可以在B中完成C to A Conversion

如过继续修改D: protected C为D :public B,那么在B中c->mem 和 d->mem都可以访问,所以D to A Conversion也能够实现。

总结


在protected和public混合继承中,如果当前的派生类可以实现Derived-to Base Conversion,那么它的直接基类和间接基类都可以实现Derived-to Base Conversion。
如果想要在当前派生类实现它派生类的Derived-to Base Conversion,例如在B中实现C to A 或者D to A,那么需要在B中能够访问C或者D中继承自A的member,简单来说就是C or D的继承必须用public。


你可能感兴趣的:(protected inheritance)