繁体   English   中英

多态性和受保护的继承问题

[英]polymorphism and protected inheritance issue

我有这段代码,并且在tp = new asp_table ,它不允许我编译说我没有访问权限。 我不明白为什么? 我试图建立一个从基类到派生类的指针,但是它不允许我这样做。 我想知道为什么。

class table {    
    int size;     
    int priority;   

    public:       
    table(int s=0,int p=0):size(s),priority(p){ }
    virtual void print(){}
};

class stud_table:public table {   
    char * name;  
    int gr;

    public: 
    void print() {cout<<"students table"<<endl;}  
    ~stud_table() {delete []name;} 
};

class asp_table:protected table { 
    char* thesis;
}; 

int main() {  
    stud_table st;   
    table * tp = &st; 
    tp = new asp_table;
    stud_table * stp = &st;   
    cout<<"Program"<<endl;    
    return 0;
} 

错误消息说明了一切:

无法将'asp_table'强制转换为其受保护的基类'table'

protected继承仅意味着asp_table及其派生类知道该继承。 因此,在类或其派生类之外不可能tp = new asp_table

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM