virtual inheritence: what does it mean?

P

pembed2003

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!
 
C

Chris Theis

pembed2003 said:
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-lite/multiple-inheritance.html#faq-25.9

HTH
Chris
 
K

Kench

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top