"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
news:9JVbb.2926$SA5.26598457@news-text.cableinet.net[color=blue]
> Until about 5 minutes ago, I was happy with my knowledge of virtual
> functions - then I read "Mixing interface and functional inheritance"
> posted by Kevin L. earlier today. All of a sudden, I found out that
> you can inherit using the virtual keyword:
>
> class A
> {
> }
>
> class B : public *virtual* A
> {
> }
>
> This is new to me and I've *never* seen it before today. I always
> assumed that a virtual base class was one that included virtual
> methods, whereas it seems class A above is such a class.
>
> So, my question is: what is a virtual base class and why use them?
> What is the effect of using them (performance, operation, etc)? What
> are the advantages and disadvantages?
>
> If you can't be bothered to give a detailed explanation, a decent web
> reference will suffice. My documentation doesn't really say anything
> about them, which is why I'm asking here.
>
> Here's hoping I'll learn something new today...
>
> Mike
>
> --
> Michael Winter
> M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)[/color]
Suppose that you have a base class B. Two classes, D1 and D2, both derive
from B. Finally, we have class F that uses multiple inheritance to derive
from both both D1 and D2.
Ordinarily, F will contain both a D1 component and a D2 component, each of
which will contain a B component. Thus F will contain, indirectly, two B
components. If B has an int member x, then the value of x in the B that is
part of D1 might be, say, 7, while the value of x in the B that is part of
D2 might be, say, 2. If you wanted to keep the x values synchonised, you
would have to set both whenever you set one.
If, by contrast, virtual inheritance is used, then there is only a single B
component within F that both D1 and D2 will share. At construction, the most
derived class F, not D1 and D2, is responsible for calling B's constructor.
The subject is discussed in most texts (e.g., Stroustrup, Lippman, and
volume 2 of Bruck Eckel's Thinking in C++
http://mindview.net/Books/DownloadSites). According to Lippman, virtual
bases can have significant performance costs.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)