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

the diamond problem

P: n/a
Hello Experts!

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

//Tony
Aug 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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)
Aug 17 '05 #2

P: n/a
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 der1:public base
{
--
};

class der2:public base
{
--
}

class sub_der:public 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.

Aug 17 '05 #3

P: n/a
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.

Aug 17 '05 #4

P: n/a
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)
Aug 17 '05 #5

P: n/a
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

Aug 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.