473,231 Members | 1,944 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,231 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 2962

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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.