inheritance increases class size

76 views Asked by At

In my experience Pattern 2 in following code nearly always fail and Pattern 1 always succeeds. What part of standard (if any) does guarantee that?

#include <iostream>
#include <cstdlib>

struct Data {
    int a;
    char b[5];
};

// Pattern 1
struct Base1 {
};

struct Derived1: Base1 {
    int a;
    char b[5];
};

static_assert(sizeof(Derived1)==sizeof(Data),  "Pattern 1 failed");

// Pattern 2
struct Base2 {
    int a;
    char b[5];
};

struct Derived2: Base2 {
};

static_assert(sizeof(Derived2)==sizeof(Data),  "Pattern 2 failed");

int main()
{
    std::cout << "Hello, world!" << std::endl;
}

Can Derived1and Data be considered structures with common beginning sequence?

0

There are 0 answers