473,320 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Virtual functions and virtual base classes - I'm confused

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
9 2966

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
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
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
"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

"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
"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
"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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Geiregat Jonas | last post by:
I've seen some c++ code where before a method there was vitual ex: class foo{ public: virtual bool method(); } what does virtual do ?
62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
2
by: david.sanderson | last post by:
Hi, Hopefully this is thecorrect group, its a bit C++ and a bit POSIX... Ive had a look in the archives, and Im a bit confused.... I have some classes in a shared library (a .so on a QNX...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
4
by: paul | last post by:
I was trying out the following sample code from Scotty Meyers: Effective C++ book: #include <iostream> class B { public: virtual void f() const { std::cout << "B::f()" << std::endl; } };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.