2016-03-05 63 views
0

我有这个代码,并在行 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; 
} 
+0

访问什么?请将完整的错误消息添加到您的问题。 – emlai

+0

错误:不允许转换为不可访问的基类“表”。 – Timur

回答

0

错误消息说,这一切:

cannot cast 'asp_table' to its protected base class 'table'

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

+0

非常感谢您的评论:(但我仍然不明白...所以基本上我不能使用多态,当派生类继承保护? – Timur

+0

是的,不在类或它的派生类之外,这就是保护继承的意思。看来你想使用公有继承。 – emlai