I have this code, and in the line tp = new asp_table
, it won't let me to compile saying I don't have access. I don't understand why? I tried to make a pointer from base class to derived class, but it wouldn't let me. I would like to understand why.
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;
}
The error message says it all:
cannot cast 'asp_table' to its protected base class 'table'
protected
inheritance means only asp_table
and its derived classes know of the inheritance. Therefore tp = new asp_table
is not possible outside the class or its derived classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.