What will be the result of the code using multiple inheritance of the structs?

#include <iostream>

struct V {
    int x;
    V(int t) : x(t) {}
};

struct A : virtual V {
    int x;
    A(int t) : V(3 * t), x(3 * t) {}
};

struct B : virtual V {
    int x;
    B(int t) : V(5 * t), x(5 * t) {}
};

struct C : A, B {
    C(int t) : V(t), A(t), B(t) {}

    void show_x() {
        std::cout << "V::x = " << V::x << std::endl;
    }
};

int main() {
    C c(1);
    c.show_x();
    return 0;
}
Explanation
12.6.2 Initializing bases and members

6. ... ... A mem-initializer naming a virtual base class shall be ignored during execution of the constructor of any class that is not the most derived class.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes