Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > the diamond problem

Reply
Thread Tools

the diamond problem

 
 
Tony Johansson
Guest
Posts: n/a
 
      08-17-2005
Hello Experts!

It it correct to say that a solution to the diamond problem is to use
virtual inheritance with virtual base classes.

//Tony


 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      08-17-2005
Tony Johansson wrote:
>
> It it correct to say that a solution to the diamond problem is to use
> virtual inheritance with virtual base classes.
>


It depends on what you mean by "the diamond problem." You use a virtual
base when you need a base object that's shared among various derived
objects. You don't use a virtual base when you don't want that.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
 
 
 
Jaspreet
Guest
Posts: n/a
 
      08-17-2005
Tony Johansson wrote:
> Hello Experts!
>
> It it correct to say that a solution to the diamond problem is to use
> virtual inheritance with virtual base classes.
>
> //Tony


Yeah, that is one of the most popular and efficient solutions to the
diamond problem.

class base
{
--
};


class der1ublic base
{
--
};

class der2ublic base
{
--
}

class sub_derublic der1, der2
{
--
}

Soo, now sub_der would have 2 copies of base, one from der1's side and
the other from der2's side. To prevent this we can have the base class
derived virtually.

Just to add, a point which is OT here but in Java, this situation never
arises since it does not support multiple inheritance. But again this
point I said above is completely off-topic.

 
Reply With Quote
 
Josh Mcfarlane
Guest
Posts: n/a
 
      08-17-2005
Jaspreet wrote:
> Just to add, a point which is OT here but in Java, this situation never
> arises since it does not support multiple inheritance. But again this
> point I said above is completely off-topic.


Well, it could also be argued that this should not occur in C++ if you
design your classes in a way that does not require it, but that's
another day, another thread.

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      08-17-2005
Jaspreet wrote:

>
> Soo, now sub_der would have 2 copies of base, one from der1's side and
> the other from der2's side. To prevent this we can have the base class
> derived virtually.
>


And just to clarify: having 2 copies of base is not a "problem" in
itself. It's a problem if it isn't what you want.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
GrahamJWalsh@gmail.com
Guest
Posts: n/a
 
      08-18-2005
and just to add, using virtual inheritance impacts the order of
construction of your objects. It's a good interview question tied in
with general questions about order of construction. Basically virtually
inherited classes take precedence before non-virtually inherited
classes. that's just FYI and one to watch if your interviewing. you
can find out more in scott meyers effective c++ and various other
books.


G

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How compiler internally slove diamond problem in C++ sharmamandy C++ 0 09-10-2010 07:01 AM
Is there any more benifits by virtual inheritance than resolving the"diamond problem" ? thomas C++ 5 11-27-2009 12:27 PM
Template -- Diamond ring Problem Pallav singh C++ 6 12-27-2007 09:50 PM
"diamond" problem jasson118@hotmail.com C++ 14 03-01-2007 09:01 PM
virtual inheritance / dreaded diamond problem Alexander Stippler C++ 0 07-14-2003 04:52 PM



Advertisments