Home > Back-end >  Why protect the pointer to the parent class inheritance and private inheritance cannot point to subc
Why protect the pointer to the parent class inheritance and private inheritance cannot point to subc

Time:09-27

The class Person
{
Protected:
string Name;
Int the Age;
Public:
Void, Update (string name, int the age, the string sex);
Void the Speak ();
};
The class Student: protected Person
{
Protected:
String every;
String ID;
The Person the Monitor;
Public:
Void, Update (string name, int the age, the string sex, string, every string id, Person& The monitor);
Void the Speak ();
};
Int main ()
{
Student s1.
Liubei. Update (18, "zhang", "male", 5 "software engineering", "112233", s1);
Liubei. Speak ();
return 0;
}
This is the main code, of which the member function implementation in order to don't seem so burdensome and then deleted, run after an Error: [Error] 'Person' is an inaccessible base of 'Student,
Then I checked the online, is that under the protection of inheritance and private inheritance, Pointers and references of the parent cannot point to subclass, but didn't explain why, so I know who told that I am just learning c + +, thank thank!!!!!

CodePudding user response:

Oh sorry, objects created inside the main function is originally liubei, worry for easy to see a little later changed to s1, careless of I forget to change the below, please automatically liu bei as s1;

CodePudding user response:

The role of protected inheritance know, I just don't say, regulation is such,
Reverse to see, in the case of protected inheritance, if the outside of the derived class can be used a base class reference or pointer to a derived class object, that can be accessed through a base class reference or pointer to kind of objects to send members of the public, this contradiction with protected inheritance,

CodePudding user response:

refer to the second floor SDGHCHJ response:
protected inheritance role know, I just don't say, regulation is such,
Reverse to see, in the case of protected inheritance, if the outside of the derived class can be used a base class reference or pointer to a derived class object, that can be accessed through a base class reference or pointer to kind of objects to send members of the public, this contradiction with protected inheritance,

But it is a to belong to one of the members of the derived class function, is not a global function, the realization of the function after the incoming parameters is also within the class, and the monitor is this member variable is originally derived classes, with member variables can point to is it?

CodePudding user response:

reference OYMN reply: 3/f
Quote: refer to the second floor SDGHCHJ response:
protected inheritance role know, I just don't say, regulation is such,
Reverse to see, in the case of protected inheritance, if the outside of the derived class can be used a base class reference or pointer to a derived class object, that can be accessed through a base class reference or pointer to kind of objects to send members of the public, this contradiction with protected inheritance,

But it is a to belong to one of the members of the derived class function, is not a global function, the realization of the function after the incoming parameters is also within the class, and the monitor is this member variable is originally derived classes, with member variables can point to is it?
compiler doesn't have time to go outside the class to judge where you put it
  • Related