Access rights to protected in C + +

Source: Internet
Author: User

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
  1. #include <iostream>
  2. Using namespace std;
  3. Class Base
  4. {
  5. Public
  6. Base () {};
  7. Virtual ~base () {};
  8. Protected
  9. int Int_pro;
  10. };
  11. Class A: Public Base
  12. {
  13. Public
  14. A () {};
  15. A (int da) {int_pro = da;}
  16. void Print (A &obj) {obj.int_pro = 24;}
  17. void PrintPro () {cout << "The proteted data is" << Int_pro <<endl;}
  18. };
  19. int main ()
  20. {
  21. A Aobj;
  22. A AObj2 (5);
  23. Aobj2.printpro ();
  24. Aobj.print (AOBJ2);
  25. Aobj2.printpro ();
  26. //NOTE 1
  27. //aobj.int_pro = 8;
  28. }

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 + +

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.