Reprinted, relieved to read C + + Primer mind puzzled: http://www.cnblogs.com/harlentan/archive/2011/03/05/2006444.html
About the access rights of protected in C + + is a very old topic, old to everyone is reluctant to discuss, think he saw to eat to sleep so natural.
When I read "C + + Primer" again, the description of protected members is this:
Protected members
The Protected Access label can thought of as a blend of private and public:
- Like private members, protected members is inaccessible to users of the class.
- The protected members is accessible to classes derived from this class.
- In addition, protected have another important property:
A derived object may access the protected members of their base class only through a derived object. The derived class has no special access to the protected members of base type objects.
In the absence of inheritance, protected is the same as private. Differentiation occurs when a class is derived.
The first two of the above paragraphs are well understood, the base class object cannot access the protected member of the base class, and the protected member of the base class can be accessed in the derived class. This means that private members cannot be inherited, only members of the public,protected can be inherited.
That's the last one. A derived class object if you want to access the base class protected members only through derived class objects, the derived class cannot access the protected members of the base class object.
Note the DriveD class and DriveD object: derived and derived class objects. The 1th and 2nd are for derived classes.
For the 3rd conclusion: only in derived classes can the protected members of the base class be accessed through derived class objects.
To give a simple example:
[CPP]View PlainCopy
- #include <iostream>
- Using namespace std;
- Class Base
- {
- Public
- Base () {};
- Virtual ~base () {};
- Protected
- int Int_pro;
- };
- Class A: Public Base
- {
- Public
- A () {};
- A (int da) {int_pro = da;}
- void Print (A &obj) {obj.int_pro = 24;}
- void PrintPro () {cout << "The proteted data is" << Int_pro <<endl;}
- };
- int main ()
- {
- A Aobj;
- A AObj2 (5);
- Aobj2.printpro ();
- Aobj.print (AOBJ2);
- Aobj2.printpro ();
- //NOTE 1
- //aobj.int_pro = 8;
- }
The results of the compilation run are as follows:
The protected data is 5
The protected data is 24
It can be seen that it is possible to access protected members directly inside derived classes and to access the protected members of the base class of the derived class object.
But if you untie note 1, you'll compile an error.
Many books say that there are derived classes in cases where the access rights of protected are the same as public. This is not true, there is no difference in the direct access of the class, but the protected member of the Access object's base class can only be inside the class.
I only cite one level of inheritance, if there are multiple inheritance cases, such as layer three. So. The inside of the middle-tier class can also access the base class members of the third-level class object, but not the members of the third-tier class's own protected.
Access rights to protected in C + +