Access Permissions of Class Members

Source: Internet
Author: User
Del. icio. us tags: C ++, protected, access

In a class, the third-layer access permission is: Only public members can be accessed from outside, its subclass can access protected and public members, and its own can access private, protected and public. However, the following example is a strange counterexample.

  1: class A
  2: {
  3: public:
  4:     A() : x(0) {}
  5:     int foo(A a)
  6:     {
  7:         return this->x + a.x;
  8:     }
  9: private:
 10:     int x;
 11: };
 12: 
 13: class B
 14: {
 15: public:
 16:     B() : y(0) {}
 17: protected:
 18:     int y;
 19: };
 20: 
 21: class C : public B
 22: {
 23: public:
 24:     C() {}
 25:     int bar(B* b)
 26:     {
 27:         return this->y + b->y;
 28:     }
 29: };
 30: 
 31: int main()
 32: {
 33:     A a1, a2;
 34:     a1.foo(a2);
 35: 
 36:     B b;
 37:     C c;
 38:     c.bar(&b);
 39:     return 0;
 40: }

During compilation, you will find that the foo call is correct, but the bar call has an error. 'Int B: y' is protected. Here the problem comes. C inherits B, so it inherits B's member Y. Why cannot it be in the form of B-> Y. Of course not. The reason is that the compiler has given that protected members can only access them directly through subclasses, for example, C-> Y. But for B-> Y, it is equivalent to external access, this is not allowed. In a: Foo, you can directly access a: X, for example,. x, but a: X is private. It should be accessible only through this-> X in the class, not through. x. get_x. The problem lies here.

In C ++, the three-layer access permission of the class is for the external class. That is to say, I have a class A. For another class B, its objects can access the public member of the objects of a. If B is a subclass of, you can also access the protected member. However, for all objects of the same class A, they can access all members. For example, a student in the same class can see everything in his class (all objects of Class A exchange all information ), students not in this class can only see the published information (all objects of Class B can only see the public information of Class ). However, the two classes share some information (protected), but they can only be viewed through specific means (access through objects of B), rather than directly (access through objects of ).

Why? If you do not have any internal security protection, you can access the service through a certain method. If you do not have any contact, you will be rejected. First, access between similar objects is the key to implementing the copy constructor. Otherwise, you cannot construct a new object through another object of A, because you cannot access private members. It may be said that as long as get is used, set is good. However, this increases the burden on function calling. If no get/set is written, nothing can be done. Therefore, everything in the class is visible. Out-of-class access, of course, with reserved access, can be effectively encapsulated to increase security.

This question comes from Yunfeng's blog. After reading his explanation, "the so-called private is for classes rather than specific objects", I did not understand it. I simply wrote it myself and asked the people around me, think about it and you will probably understand it.

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.