Detailed description of the access permissions of C ++ protected members

Source: Internet
Author: User

Access permission for C ++ protected members
The discussion on the access permissions of protected in C ++ is already a very old topic, and it is so old that everyone is reluctant to discuss it.
When I read C ++ Primer again, the description of the protected member is as follows:
Protected Members
The protected access label can be thought of as a blend of private and public:
Like private members, protected members are inaccessible to users of the class.
Like public members, the protected members are accessible to classes derived from this class.
In addition, protected has another important property:
A derived object may access the protected members of its base class only through a derived
Object. The derived class has no special access to the protected members of base type objects.
Without inheritance, protected is the same as private. Differentiation occurs only when the derived class is used.
The first two articles in the above section are well understood. The base class object cannot access the protected member of the base class, and the derived class can access the protected member of the base class. In other words, private Members cannot be inherited. Only public and protected members can be inherited.
This is the last one that is somewhat confusing. If you want to access the base-class protected member only through the derived class object, the derived class cannot access the protected member of the base class object.
Please note that drived class and drived object are derived classes and derived class objects. The first and second points are for the derived classes.
To sum up the third sentence: only in a derived class can the derived class Object Access the protected member of the base class.
[Cpp]
# 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 compilation and running results are as follows:
The protected data is 5
The protected data is 24
It can be seen that it is feasible to directly access the protected member within the derived class and access the protected member of the base class of the object in the derived class.
However, if Note 1 is unfixed, an error will be reported during compilation.
In many books, when a derived class exists, the access permission of protected is the same as that of public. This statement is incorrect. Direct access within a class is no different, but the protected members accessing the object base class can only be inside the class.
I only list the situations where only one layer of inheritance exists. If there are multiple inheritance cases, such as three layers. So. The middle-layer class can also access the base class members of the third-layer class object, but cannot access the protected members of the third-layer class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.