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

virtual inheritence: what does it mean?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
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

P: n/a

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...
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 discussion thread is closed

Replies have been disabled for this discussion.