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

Diamon inheritance problem

P: n/a
hi,

can diamond inhertance problem be solved using virtual functions,, or
can only be done with scope resolution operators.. is there any other
way to solve this problem...

Thanks,
suri

Jan 10 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a

su*********@gmail.com wrote:
hi,

can diamond inhertance problem be solved using virtual functions,, or
can only be done with scope resolution operators.. is there any other
way to solve this problem...


Virtual base classes.
http://www.parashift.com/c++-faq-lit....html#faq-25.8
and others in the same section might be useful

Jan 10 '06 #2

P: n/a
* su*********@gmail.com:

can diamond inhertance problem be solved using virtual functions,, or
can only be done with scope resolution operators.. is there any other
way to solve this problem...


Diamond inheritance is when a class Derived inherits twice or more from a
class Base, and you want only one Base sub-object in each Derived object.

Virtual functions have nothing to do with that, and scope resolution
operators have nothing to do with that.

The C++ language support for this problem is virtual inheritance.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 10 '06 #3

P: n/a

<su*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
hi,

can diamond inhertance problem be solved using virtual functions,
No. They won't help.
, or
can only be done with scope resolution operators..

No. That won't help either.
is there any other
way to solve this problem...


There is only one way: Look up 'virtual inheritance'.

-Mike
Jan 10 '06 #4

P: n/a
su*********@gmail.com wrote:
hi,

can diamond inhertance problem be solved using virtual functions,, or
can only be done with scope resolution operators.. is there any other
way to solve this problem...


When inheriting from more than one base, make sure that the bases are
pure abstract. I.e., only do multiple _interface_ inheritance.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 10 '06 #5

P: n/a
dc
I will disagree with those who said diamond inheritence cannot be
solved using scope resolution operators.
Only thing is u need be careful and code become clumpsy.

Jan 10 '06 #6

P: n/a
dc
Also this can be solved using class ptrs, But here it will restrict the
dereferencing of members
only via base ptrs.
Obviously the best way is virtual inheritence.

Jan 10 '06 #7

P: n/a
dc wrote:
I will disagree with those who said diamond inheritence cannot be
solved using scope resolution operators.
And yet, you replied to me, the only person who did not comment on them?
Only thing is u need be careful and code become clumpsy.


Well that would be reason enough to avoid it.

If by "scope resolution operators" you mean to describe the grandparent
base required by accessing it via a particular parent, you have not
solved the problem of multiple bases, merely specified which base you
are referring to.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 10 '06 #8

P: n/a
dc wrote:
Also this can be solved using class ptrs, But here it will restrict the
dereferencing of members
only via base ptrs.
I'm not following, care to post code?
Obviously the best way is virtual inheritence.


In almost all cases you can avoid the diamond design problem completely
with careful planning. You really want to do your utmost to avoid it,
rather than working around it. Ideally, the bases you inherit from
should be orthogonal.

If you describe the problem you are trying to solve, we will be able to
help.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 10 '06 #9

P: n/a
dc wrote:
I will disagree with those who said diamond inheritence cannot be
solved using scope resolution operators.
Only thing is u need be careful and code become clumpsy.


That depends on what you think the "diamond inheritance problem" is. If
you derive from the same base type multiple times because you want
multiple copies of that base type in the derived type then the usual
pattern of inheritance will work just fine. If that's not want you want
then you have to plan ahead, using virtual inheritance to insure that
only one copy of the base object exists, using scope resolution
operators to access only one of the copies of the base object, or using
a stateless base type so that it doesn't matter which copy you get.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.