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 English above are very easy to understand,The base class object cannot access the protected member of the base class.,You can access the protected member of the base class in the derived 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 following sentence:Only in a derived class can the derived class Object Access the protected member of the base class..

 
# 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.

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.