views:

83

answers:

1

Possible Duplicate:
In C++ virtual base class?

The virtual keyword is used to remove the ambiguity, but how does it actually work?

I have four classes: a, b, c, and d.

I am doing:

class a
{public int a;};

class b: virtual public a
{};

class c: virtual public a
{};

class d:public b,public c
{};

This works, but what actually happens?

+1  A: 

In your example, since 'a' is a virtual base class, there exists only one subobject of type 'a' in the object of type 'd'.

In the case of virtual base classes, the most derived class (e.g. if you create an object of type 'd') is responsible for invoking the constructor of the virtual base class 'a'.

Chubsdad