Objects with Inheritance Storage Memory

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 an object of class C, are variables stored in a certain way? I know what happens in a POD object, but it is not a POD object ...

+5
source share
2 answers

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

The order in which the subobjects of the base class are distributed in the derived object itself (1.8) is not specified.

So, in your example, Ceach object has a subobject of the base class of the type Aand a subobject of the base class of the type B, but whether the base element has a base element Abefore or after B.

+2
source

, , , : A:: k, A:: m, B:: k, B:: m, C:: k, C:: m (, ). , , , , C B, () , cast ( , , reinterpret_cast < > ).

0

All Articles