Sobes.tech
Junior — Senior

Determining the sequence of constructor and destructor calls in multiple inheritance

livecode

Task condition

In the function check_call_order, an object Current is created, which inherits from ABase and BBase and contains two member fields — BField and AField. It is required to determine the order in which the constructors will be called when constructing the Current object.

struct ABase {
    ABase()  { std::cout << "ABase\n"; }
    ~ABase() { std::cout << "~ABase\n"; }
};

struct BBase {
    BBase()  { std::cout << "BBase\n"; }
    ~BBase() { std::cout << "~BBase\n"; }
};

struct AField {
    AField()  { std::cout << "AField\n"; }
    ~AField() { std::cout << "~AField\n"; }
};

struct BField {
    BField()  { std::cout << "BField\n"; }
    ~BField() { std::cout << "~BField\n"; }
};

struct Current : ABase, BBase {
    BField b;
    AField a;

    Current()  { std::cout << "Current\n"; }
    ~Current() { std::cout << "~Current\n"; }
};

void check_call_order() {
    Current current;
}