Computing.Net > Forums > Programming > C++ - virtual base class

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ - virtual base class

Reply to Message Icon

Name: Gargamel
Date: October 12, 2005 at 17:46:22 Pacific
OS: windows XP pro + sp1
CPU/Ram: P4 - 1.8GHz, 512MB ddram
Comment:

Hi everybody.
Say I have a class A which has a virtual (not abstract) function f. Now, this class is inherited by class B which overrides f.
What's exactly the difference between the two following class declarations:

class B : public A
class B : public virtual A

Thanks in advance,
Itai.



Sponsored Link
Ads by Google

Response Number 1
Name: governortyrran
Date: October 17, 2005 at 16:53:06 Pacific
Reply:

Simply put: in the above case nothing.

Consider virtual inheritance as a way of morphing an inheritance graph. So in your example your virtual inheritance is ubiquitous.

An explanation:

ex: (non virtual)

class Inheritable;
class DerivedA : public Inheritable;
class DerivedB : public Inheritable:
class Common : public DerivedA, public DerivedB

In this inheritance graph both DerivedA and DerivedB have to implement their own unique instance of Inheritable. Therefore, if Common tried to use a member function (or variable) of Inheritable, the compiler would need to know whether to lookup the function in either the DerivedA or DerivedB classes, since both these classes have valid (identical code, yet separate) function implementations. The compiler would give an ambiguity error if you tried to call Inheritable::AFunction() from inside Common. This is fixed by specifying either DerivedA::AFuntion() or DerivedB::AFuntion() when calling AFunction inside Common.

ex: (virtual)

class Inheritable;
class DerivedA : public virtual Inheritable;
class DerivedB : public virtual Inheritable:
class Common : public DerivedA, public DerivedB

Now Inheritable is inherited virtually. The inheritance graph is changed so that DerivedA and DerivedB share the same implementation of Inheritable. I.E. there is only onle implmentation of Inheritable. This resolves the ambiguity described above: Inheritable::AFunction() is a valid call from Common. However, now you need to now that DerivedA and DerivedB can each change the state of Inheritable (i.e. member variables). Therefore you need to make sure that DerivedA and DerivedB dont unexpectedly interfere with each other.

As far as the virtual function is concerned in the above virtual example, if DerivedA and DerivedB both override a virtual function in inheritable, an ambiguity issue will arise similar to above non-virtual example.

Essentially, to keep things simple, dont virtually inherit a class with virtual functions that are overriden in the derived classes. (Read that ten times fast.)

invalid namespace anyone


0

Response Number 2
Name: Gargamel
Date: October 18, 2005 at 08:26:10 Pacific
Reply:

Hi.

That is some detailed, very easy to understand, explanation.

Thanks a million :-)

Itai.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


SQL Insert Query question C++ String Project?



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ - virtual base class

C++ virtual member function www.computing.net/answers/programming/c-virtual-member-function/10468.html

c++: Templates versus virtual functions www.computing.net/answers/programming/c-templates-versus-virtual-functions/1368.html

Simple design questions in C++ www.computing.net/answers/programming/simple-design-questions-in-c/4384.html