Behind the scenes of public, private and protected

心不动则不痛 提交于 2021-01-25 20:50:33

问题


I try to dive deeper and understand the differences between Public | Private | Protected in a low level perspective, in C++.

How are the differences between the three expressed in the memory?


回答1:


private, public and protected does not cause members to be stored in specific regions of memory. The access is checked by the compiler. On the very lowest level, there is no difference.

However, access specifiers do have an effect on what guarantees you get on the order in which class members are layed out in memory.

From the C++17 standard draft:

Nonstatic data members of a (non-union) class with the same access control (Clause [class.access]) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause [class.access]). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions ([class.virtual]) and virtual base classes ([class.mi]).

This means, that for

 struct foo {
     private:
        int x;
     protected:
        int a;
        int b;
     public:
        int m;
        int n;
     private:
        int y;
};

You only get the guarantee that in memory x comes before y, a comes before b and m comes before n. Other than that, the order in which the members are layed out in memory is unspecified.

However, rarely the order of members in memory is a useful information. Hence it isn't too wrong to say that access specifiers have nothing to do with "low level memory".




回答2:


At the lowest level (byte representation of objects) there are absolutely no difference between public, private and protected. At most compilers can (but are not required to) reorder members according to their visibility.

At intermediary level (run time behaviour) there is little if any difference. If you can find a public pointer to a private data you can safely use it. Specifically this is different from constness where using a non const pointer to alter const data is explicitely Undefined Behaviour and can cause SIGSEGV errors.

The difference is only at the highest level. You can use public members from anywhere, while private members can only be used in the class where they are declared and protected member can be used from their class and all classes inheriting it - but friendness can allow specific classes of function to access private or protected data.




回答3:


Not at all.

The access is "granted/denied" by the compiler.

Any access not matching an appropriate visibility (as controlled by the class of the accessed object, via pointer or not) is prevented before building.

Note:

The other answers usefully discuss the effect of visibility on order in memory.

I howevr answered the different question I read into OPs post "How are memory protection features used to implement member accessability/visibility?", which concerning cause and effect is kind of the reverse question. Or only the ordering/structuring of the members in differently configured memories is the tool to achieve the desired visibility effect, which in turn would require (but not cause) members to be ordered in a certain way.

I.e. I do not see a conflict between answers, just a different interpretation of the question.



来源:https://stackoverflow.com/questions/65199837/behind-the-scenes-of-public-private-and-protected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!