As far as i know, virtual ensures that only one copy of the properties of base class is inherited to the derived class. And in a multiple inheritance the constructor of all base is called first then the constructor of derived is called. In the following code why was the constructor of class B called twice in a row? To my understanding the output should have been
B
B1
B2
D
but the output came out to be
B
B
B1
B2
D
Here's the full code.
#include <iostream>
class B
{
public:
B(){std::cout << "B" << std::endl;}
};
class B1:public B
{
public:
B1(){std::cout << "B1" << std::endl;}
};
class B2:virtual public B
{
public:
B2(){std::cout << "B2" << std::endl;}
};
class D:public B1, public B2
{
public:
D(){std::cout << "D" << std::endl;}
};
int main()
{
D d1;
}