473,790 Members | 3,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual inheritence: what does it mean?

Hi all,
I recently saw a piece of code that looks like:

class one{public: one(){} };
class two : public virtual one{public: two(){} };
class three : virtual public one{public: three(){} };

My questions:

1. What does it mean to have a virtual inheritenec? When do you want
that? What affect does it have on class two and three?

2. It seems like you can say "public virtual" or "virtual public".
Does order matter?

Thanks!
Jul 22 '05 #1
2 2167
This helps in multiple inheritance. If class four :public two, three ..
then class four will have only one instance of class one variables/methods,
no ambiguity.
check out C++ FAQs
"pembed2003 " <pe********@yah oo.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
Hi all,
I recently saw a piece of code that looks like:

class one{public: one(){} };
class two : public virtual one{public: two(){} };
class three : virtual public one{public: three(){} };

My questions:

1. What does it mean to have a virtual inheritenec? When do you want
that? What affect does it have on class two and three?

2. It seems like you can say "public virtual" or "virtual public".
Does order matter?

Thanks!

Jul 22 '05 #2

"pembed2003 " <pe********@yah oo.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
Hi all,
I recently saw a piece of code that looks like:

class one{public: one(){} };
class two : public virtual one{public: two(){} };
class three : virtual public one{public: three(){} };

My questions:

1. What does it mean to have a virtual inheritenec? When do you want
that? What affect does it have on class two and three?

2. It seems like you can say "public virtual" or "virtual public".
Does order matter?

Thanks!


In short it helps to avoid duplicate base objects in a so called "diamond
shaped" inheritance pattern. For a more elaborate answer you should check
out the FAQ where it is covered in all detail.

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

HTH
Chris
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1978
by: dcipher | last post by:
I'm in the process of rewritting my graphics library to be purely OO using C++. The following is a section of my heirarchy of classes: PImage // defines all basic graphics routines as virtual functions PImage8 // implements those routines for 8bpp PImage15 // and for 15bpp PImage16 // and 16bpp PImage24 // and so on...
5
4091
by: alex goldman | last post by:
When compiling this code with g++ -Wall -pedantic -ansi -c program.cpp # this is GCC-3.3.4 ###################################### #include <vector> class b { public: virtual void f() = 0; };
175
8910
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
14
1930
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
5
3783
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in C++ ? waitng for the answers.
2
1870
by: Heinz Ketchup | last post by:
Hello, I'm looking to bounce ideas off of anyone, since mainly the idea of using Multiple Virtual Inheritance seems rather nutty. I chalk it up to my lack of C++ Experience. Here is my scenario... I have 5 Derived Classes I have 3 Base Classes
7
3412
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case class is derived as virtual? How does the order of construction/destruction is impacted if the base class is derived as virtual or non virtual? just see the example below.
7
1786
by: v4vijayakumar | last post by:
Is it possible to implement member object's virtual functions, in the containing class? If not, is it possible to simulate this behavior? ex: class test { protected: virtual void fun() = 0; };
3
2210
by: Juha Nieminen | last post by:
It occurred to me while developing an application: What happens if two (completely independent) base classes have the exact same virtual function, and then a derived class is derived from both, and this derived class implements that function? Will it work? Will it cause an error along the lines of "ambiguous function definition" or whatever? Well, I tried it, and it actually seemed to compile and work just as expected. Apparently this...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5424
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.