Class Member Access Qualifiers and Binary Code

I understand what typical access specifiers are and what they mean. "Public" members are available anywhere, 'private' members are available only to one class and friends, etc.

What I am interested in is that, if something, it equates to lower levels. Are there any functional differences after compiling between them beyond the high-level restrictions (which can get access to what) imposed by the language (in this case, C ++). They are used.

Another way to express this is if it were an ideal world where programmers always made good choices (for example, not referring to members who can change later and using only clearly defined members that should remain unchanged between implementations), whether they Any reason to use these things?

+3
source share
5 answers

Access specifiers exist only for compilation purposes. Any memory within your distribution of programs can be accessed by any part of the executable file; no public / private concept at run time

+8
source

Michael answers correctly. Access specifiers do not directly affect the received code.

/, .

class A {
private:
    int x;
};

class B {
protected:
    int x;
};

class C : public A, public B {
public:
    int &get_x() { return x; } // only B::x is accessible, no error.
};

, .

+1

, . , , .

0

, . - , , .

. , c++ -Dprotected=public -Dprivate=public file.cc, ( , ).

0

(), "public" "private" ( , ..). ( ), , . \ ( , ) , , . , , .

, , , - private, , ( , , ). , ? ? ? () , , ( ).

0

Source: https://habr.com/ru/post/1743852/


All Articles