By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Virtual functions and virtual base classes - I'm confused

P: n/a
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)
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

Michael Winter wrote:
[...]
So, my question is: what is a virtual base class and why use them?


Read up on "diamond of death".

regards,
alexander.
Jul 19 '05 #2

P: n/a
Michael Winter wrote in news:9JVbb.2926$SA5.26598457@news-
text.cableinet.net:
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?
So that when you have a class that derives from 2 or more bases, where
2 or more of those bases derive (vurtually from) another class that
"other" class can be merged, so that the one we are declaring only
containes *one* instance of the "other" base class.

continueing from your example:

class C: public virtual A {};

class BC: public B, public C {};

Assuming there is no padding/allignment we should now have:

sizeof( BC ) == ( sizeof(B) + sizeof(C) - sizeof(A) ).
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.
http://www.parashift.com/c++-faq-lit...html#faq-25.11

I put [virtual base] into the search box at:

http://www.parashift.com/c++-faq-lite

And this was the second link, the faq is well worth the read.

Here's hoping I'll learn something new today...


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

P: n/a
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.
virtual inheritance pervent multiple copies a base class in a multiple
inheritance scenario.

For example:

class B
{
};

class D1 : public B
{
};

class D2 : public B
{
};

class DD : public D1, public D2
{
};

Instances of class DD have essentially two instances of class B, all
members of class B are duplicated because both D1 and D2 are derived
from B. By changing the class definition of D1 and D2 to :

class D1 : virtual public B
{
};

class D2 : virtual public B
{
};

class DD will now have only one instance of class B.
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?


Item 43 of the (highly recommended) book "Effective C++" from Scott
Meyers answers your questions regarding this topic.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #4

P: n/a
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
news:9J*********************@news-text.cableinet.net
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)


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)

Jul 19 '05 #5

P: n/a

"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message
news:9J*********************@news-text.cableinet.net...
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


Let's consider the following inheritance scheme:

class CBase {...};

class CChild1 : public CBase {... };

class CChild2: public CBase { ... };

which could come from a library for example. Now you want to create a class
which derives from CChild1 and CChild2

class CMyObj : public CChild1, public CChild2 {...};

If you draw an inheritance scheme you'll see that this structure resembles a
diamond and your class will end up with two subobjects of the CBase class.
In case you want to upcast to a CBase object it will get tricky because the
compiler has no way to figure out which subobject (remember you have 2 of
them due to multiple inheritance) to use. The solution to this problem is a
language extension in the way of providing the possibility to use virtual
with inheritance. If you inherit from a class as virtual, only one subobject
of that class will appear as a base class. Hence there is no ambiguity
during upcasting.

HTH
Chris
Jul 19 '05 #6

P: n/a
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<9J*********************@news-text.cableinet.net>...
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


Joy of joys, it's in the FAQ. Take a deep breath ...

http://www.parashift.com/c++-faq-lit...heritance.html

hth
GJD
Jul 19 '05 #7

P: n/a
"Gavin Deane" <de*********@hotmail.com> wrote in message
news:6d**************************@posting.google.c om...
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<9J*********************@news-text.cableinet.net>...
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


Joy of joys, it's in the FAQ. Take a deep breath ...


Yes, I realised this when I decided: "I really should get a copy of
the C++ Language Standard, rather than relying on Microsoft's
sometimes brief version of it". I looked in the FAQ for places to
obtain it and I found the entry.

Sorry for breaking a cardinal rule,

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
http://www.parashift.com/c++-faq-lit...heritance.html

hth
GJD

Jul 19 '05 #8

P: n/a
"Michael Winter" <M.Winter@[no-spam]blueyonder.co.uk> wrote in message news:<9J*********************@news-text.cableinet.net>...
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


Virtual function is a function whos address is determined at runtime,
rigth? Based not on the variable (pointer/reference type), but rather
on the type of the actuall object. Example:

class B {
virtual foo() {};
};

class D : public B {
virtual foo() {};
};

B *p = new D;
p->foo();

Here, even though the type on p is B pointer, it points to object of
type D, hence D's foo is called... virtual function, determined at
runtime.

Now, for the virtual classes.

Best example is dimond shaped inheritance, something like this:

class Base {
int variable;
};

class Derived1 : public Base {
};

class Derived2 : public Base {
};

class Final : public Derived1, public Derived2 {
};

Now, base class has a variable "variable". Both Derived1 and Derived2
inherit from base, hence objects of type Derived1 or Derived2 "have"
this instance variable as well. So, the class Final inherits from both
Derived1, and Derived2. This means that class Final now has two
variables, and their fully qualified names are: Derived1::variable and
Derived2::variable. You DO get two copies, because you did not use
virtual inheritance.

Now imagine that the code gets those two changes:

class Derived1 ; virtual public Base {};
class Derived2 : virtual public Base {};

Everything stays the same. Now, since Base is a virtual base class for
ALL derived types, you can now do:

class Final : public Derived1, public Derived2 {};

Now, Finall class will have only ONE copy of "variable".

Hope that makes sense.

Read up on virtual ingeritence and dimond shaped inheritance.

Martin
Jul 19 '05 #9

P: n/a
"Michael Winter" wrote on 23 Sept 03:
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


Thanks everyone for your replies, and sorry once again for posting
something that was already in the FAQ.

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)
Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.