multiple inheritance; consistency of 'virtual'

M

m0shbear

How come with
class A{};
class B: public A{};
class C: public virtual A, public virtual B{};

, B needs to virtual inherit A so that ambiguity in C wrt members of A
can be resolved? Is this due to subtleties of vtable generation when
MI is used?
 
J

Juha Nieminen

m0shbear said:
How come with
class A{};
class B: public A{};
class C: public virtual A, public virtual B{};

, B needs to virtual inherit A so that ambiguity in C wrt members of A
can be resolved? Is this due to subtleties of vtable generation when
MI is used?

Without virtual inheritance there is an ambiguity because both B and
C inherit from A, while C also inherits from B. If you tried it without
virtual inheritance, you would get errors like:

error: 'A' is an ambiguous base of 'C'

Making C inherit from A makes little sense in this case. If you really
wanted to do that, for whatever strange reason, it would have to be done
like this:

class A {};
class B: virtual public A {};
class C: virtual public A, public B {};

However, the above is just effectively doing the same as:

class A {};
class B: public A {};
class C: public B {};

just less efficiently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top