7

Does the C++ standard dictate the compilation layout of the class and struct? How are they compiled differently especially if they are empty?

unj2
  • 52,135
  • 87
  • 247
  • 375
  • Absolutelu "no" on both counts to the first question. A class has an implicit "this" pointer in answer to the second question. – paulsm4 Jun 04 '12 at 20:52
  • 11
    @paulsm4: Your answer to the second question is wrong. there is no dofference between a `class` and a `struct` in this regard. To wit, a `struct` can have `virtual` members. – John Dibling Jun 04 '12 at 20:53
  • 5
    Classes and structs are the exact same thing, the only difference is with default access specification (for members and bases). – GManNickG Jun 04 '12 at 20:53
  • @paulsm4 If there is no code there is nowhere for the implicit 'this' pointer to be generated. – user207421 Jun 04 '12 at 23:22

1 Answers1

13

It does in a way, it says that it has to allocate space for it unless certain cases when its used as a base class (known as Empty Base Class Optimization). This is to guarantee that different objects have different addresses.

They are compiled the same given that struct and class are the same thing, except for the default access specifier. In C++11 the notion of standard-layout classes/structs is introduced, and guarantees that the memory layout for empty classes to be the same.

K-ballo
  • 80,396
  • 20
  • 159
  • 169