Bandicoot
Bandicoot

Reputation: 3949

c++ implementation of access specifiers

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?

Upvotes: 3

Views: 718

Answers (4)

Bill Door
Bill Door

Reputation: 18926

C++ is a statically typed language. Similarly many of the concepts in C++ are also statically checked. Specifically, access specifiers are a checked at compiled time.

The standard gives some guarantees concerning how the members of a class are laid out in memory. The most useful is that members specified together in the same access specifier section will be available in the specified order. (This is characteristic is quite useful for accessing network packets for example.)

To answer your question, there is no "translation" to a struct (a struct is a degenerate class where the default access is public instead of private). Access specifiers are enforced at compile time. There is no run time enforcement of access specifiers.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

What's your source? I think it refers to how the objects are represented in memory, but it's the compiler, not the run-time, that deals with access specifiers.

In memory, a class might look the same as a struct, but to the compiler they do not.

Also, the two don't have to be equivalent even in memory (much like they are not equivalent for the compiler). The compiler can re-arrange the members, as long as it keeps those with no interleaving access specifiers grouped together. So, in memory,

class Object {  
public: 
   int i1; 
   int i2; 
private: 
   char i3; 
   int i4; 
};

could theoretically be represented as

struct {
   char i3;
   int i4;
   int i1;
   int i2;
}

Note that I'm only talking about the memory layout here. The actual struct equivalent of your class is:

struct Object {  
private:
public: 
   int i1; 
   int i2; 
private: 
   char i3; 
   int i4; 
};

Upvotes: 0

Matthieu M.
Matthieu M.

Reputation: 299880

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.

Upvotes: 5

Puppy
Puppy

Reputation: 146930

It doesn't. Go ahead, do a reinterpret_cast and manually index the pointer. This is for the same reason that both C and C++ allow for const to be cast away.

However, generally speaking, it's an idiotic idea to do so and C++ effectively enforces access modifiers by simply checking the modifier when you access in the normal way.

Upvotes: 3

Related Questions