Possible Duplicate:
In C++, what is a virtual base class?
In this code when an object of DR is created, the string "Hello World" should be printed 4 times, instead it is printed just 3 times.Why is it so ? From what I guess it is because of the mid1 and mid2 being virtually inherited. Can somebody explain me what happens when we virtually inherit a class and more importantly when it is useful and why ?
#include <iostream>
struct BS
{
BS()
{
std::cout << "Hello World" << std::endl;
}
unsigned int color;
};
struct mid1 : virtual public BS { };
struct mid2 : virtual public BS { };
struct mid3 : public BS { };
struct mid4 : public BS { };
struct DR : public mid1, public mid2,
public mid3, public mid4 { };
int main(int argc, char** argv)
{
DR d;
return 0;
}