0
votes
class A{
fct1(){}; 
};

class B:public A{
fct2(){};  
};
// B b; 
// A* a = &b; good! possible!!

class A{
fct1(){}; 
};
class B:protected A{
fct2(){};  
};
//B b;
// A* a=&b; error!

why is that?

What does protected inheritance have to do with pointer?

I learned that protected inheritance changes public area to protected area, so that only derived class can access its member functions and variables. :(

Please explain the principle and reason.

1

1 Answers

0
votes

The whole point of access control is to ... control who gets access to which parts of a class. "Protected" means that only derived classes have access.

The access level on a base class determines access to the base subobject. Putting this together, this means that only derived classes have access to protected base subobjects. So the conversion of &b to a pointer to A is not allowed outside classes derived from A.