objects hierarchy problem

V

vfunc

Say you have object A and derived objects B and C, with B having a
member x,
and C having a member y. Then say we want an object D to also be
derived from A but containing both members x and y. Is there a way to
declare D using both B and C, like a "joint" object ? :eek:
 
N

Neelesh Bodas

Say you have object A and derived objects B and C, with B having a
member x,
and C having a member y. Then say we want an object D to also be
derived from A but containing both members x and y. Is there a way to
declare D using both B and C, like a "joint" object ?


You can use multiple inheritence
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html

class D: public B, public C
{
....
};

If A is a virtual base class of B and C then there will be only one A
subobject inside every instance of D. Otherwise there will be two
A-subobjects inside any instance of D.

Aside - AFAIK, we usually say "Class B is derived from A", not "Object
B is derived from Object A". Object is an instance of a class.
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Say you have object A and derived objects B and C, with B having a
member x,
and C having a member y. Then say we want an object D to also be
derived from A but containing both members x and y. Is there a way to
declare D using both B and C, like a "joint" object ? :eek:

<nitpick on>
You can not derive object B from object A.
Only classes can derive from (or better: inherit) each other
<nitpick off>

Stefan
 
B

Ben Pope

Say you have object A and derived objects B and C, with B having a
member x,
and C having a member y. Then say we want an object D to also be
derived from A but containing both members x and y. Is there a way to
declare D using both B and C, like a "joint" object ? :eek:

An obvious question is why?

Do you want to be able to replace B and C objects with a D? (i.e., use
D polymorphically through pointers to B or C)

Then multiple inheritance is the answer, but be careful. Read and
understand the whole FAQ section on Multiple Inheritance.


Do you want to be able to use the public interface of both B and C?
Do you just want to make use of the implementation of both?

In each case the following will suffice and you'll likely have less
trouble than with mutliple inheritance.

class D {
B b_;
C c_;
}



Ben Pope
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top