From what I understand, non-static data members in a c++ class are packed into a C-style struct. Ignoring virtual functions and inheritance for the sake of simplifying this discussion, how are access-specifiers enforced in such a scheme ?
say a class:
class Object
{
public:
int i1;
int i2;
private:
char i3;
int i4;
};
translates to:
struct {
int i1;
int i2;
char i3;
int i4;
}
How does c++ ensure that private members i3 and i4 can not be accessed outside the class but i1 and i2 can be?
C++ has (some) safe guards to protect against Murphy, not Machiavelli.
What this means is that the const, volatile and access-qualifiers are checked at compilation time, but even then can be bypassed (with a variety of tricks).
So... C++ does not require implementing a protection scheme. If the program compiled, it is deemed correct (wrt those qualifiers) and will be executed without runtime checks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With