C + + Access Control

Source: Internet
Author: User

In C + +, access control works on, not on per-class basis per-object basis .
Important things to say three times, so ...

1. In C++ , the access control capability is the nature of the class, not the nature of the object.
2. In C++ , the access control capability is the nature of the class, not the nature of the object.
3. In C++ , the access control capability is the nature of the class, not the nature of the object.

As in Reading <<Inside the object model>> a book, there is a little bit of confusion in the place to read the <<C++ Primer>> relevant chapters in the class, see the Member access control that chapter, suddenly think of a problem for many years to confuse themselves. Then the Internet search answers, the answer is fragmented, and a little unintelligible. I saw a bull in the know and said a word:

Access ability is the nature of the class, not the nature of the object.

At first glance I did not understand, and then I have been repeatedly thinking about the meaning of this sentence, but in the absence of examples, it is difficult to understand something. Later turned over the wall to Stack Overflow search the answer, someone raised the same question, on the study, and finally a little understanding of the sentence, afraid of their own forgotten, so write down the blog.

First, establish a basic point of view and then explain it with examples.
publicunder inheritance, the public member of Base class becomes the public member of the derived class, so it can be analogous to the other two members.

So I look at an example:

 #include <iostream>   #include <iostream>  class  trival  { private : int  val; public : Trival (const  trival& another) {V Al = Another.val; //!!!     }};

Why is our object another accessible to private members? In fact, I was very early puzzled about this, but the level of lack, the question is not, and at that time the rules of the book is so written.
According to the rules of the book, Val is a private data member, our another members cannot directly access it, but we are accustomed to defining the copy constructor.
This place is my own understanding of the deviation. I think the ability to access is the nature of the object. In other words, if a data member is private, then our object cannot be accessed directly. However, this understanding is problematic, and we cannot explain the access operations to private or protected members within the class. So I'll start by saying that access control is the nature of the class, not the nature of the object. Now we can understand it. Since it is the nature of the class, the class is the basis for distinguishing whether it can be accessed. As long as it is inside the class, we can access it, not to say that the object cannot directly access the private member.
After the above explanation, we should have some perceptual understanding. The
throws a problem that is still related to access control.

#include <iostream>class Trival{protected:    int val;};class Derived :public Trival{public:    voidfunc(Trival* t1,Derived* d1){        //t1->val;报错。        d1->val;        val;    }};

Can think about why the error, if you can understand the beginning of the sentence should be no problem.
Why are the two at the bottom right?
I guess I don't have to explain too much. A simple word. For the above t1 , we can not directly access our own private members val , because we are not in the Trival class, so the direct access is not legal. But this form often misleads us into thinking that access to a member is the object's nature.

To sum up, our distinction should be based outside the class. That is, the access capability is the property of the class.
If I can not let you understand, please refer to the original text or discuss with me, my current understanding may still be deficient.

Stakc Overflowthe link below is attached
Poke me in.
Consider the wall too thick to be a porter.

In public inheritance:
All public members of the Base class become public members of the derived class &
All Protected members of the Base class become Protected members of the Derived Class.
As per the above rule:
Protected member X from A becomes protected member of Class B.
Class B can access its own protected member function Foo if it can only access members of A through which It was derived A classes.
In this case, class B contains a pointer A, It cannot access the protected members of this contained class.
Why can the B::foo () access the members of the contained class B pointer b?
The rule is:
In C + + access control works on per-class basis, not on per-object basis.
So an instance of Class B is always has access to all the members of another instance of Class B.
Below is a example:

#include <iostream>classMyClass { Public: MyClass (Const STD::string& data): Mdata (data) {}Const STD::string& GetData (ConstMyClass &instance)Const{returnInstance.mdata; }Private:STD::stringMdata;};intMain () {MyClass A ("Stack"); MyClass B ("Overflow");STD::cout<<"B via a ="<< A.getdata (b) <<STD:: Endl;return 0;}

C + + Access Control

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.