Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objects with inheritance memory storage

Tags:

c++

Say that i have some classes like this example.

class A {
     int k, m;
public:
     A(int a, int b) {
          k = a;
          m = b;
     }
};

class B {
     int k, m;
public:
     B() {
          k = 2;
          m = 3;
     }
};

class C : private A, private B {
     int k, m;
public:
     C(int a, int b) : A(a, b) {
          k = b;
          m = a;
     }
};

Now, in a class C object, are the variables stored in a specific way? I know what happens in a POD object, but this is not a POD object...

like image 344
nikitas350 Avatar asked Nov 06 '22 03:11

nikitas350


1 Answers

In the introduction of Chapter 10, Derived classes, the C++ Standard mentions:

The order in which the base class subobjects are allocated in the most derived object (1.8) is unspecified.

So, in your example C objects each have a base class subobject of type A and a base class subobject of type B, but whether the A base member comes before or after the B base member is unspecified.

like image 195
Daniel Trebbien Avatar answered Nov 15 '22 05:11

Daniel Trebbien