Another composition

F

fog

class A has its children/derived classes B and C. class D is supposed to be
contained inside both B and C.

Is the code right for the above requirement?
class A
{
D d;
};

class B : public A;
class C : public A;

Now should all public and protected members in D be accessible to all
members of B and C?
Is this a good design?
Thanks for your help!
 
C

Claudio Puviani

fog said:
class A has its children/derived classes B and C. class D is supposed to be
contained inside both B and C.

Is the code right for the above requirement?
class A
{
D d;
};

class B : public A;
class C : public A;

Now should all public and protected members in D be accessible to all
members of B and C?
Is this a good design?

Good design limits the visibility of the internals of a class as much as
possible, even to its descendents. Therefore, no, exposing the internals of A
would not be good design.

Claudio Puviani
 
J

John Harrison

fog said:
class A has its children/derived classes B and C. class D is supposed to be
contained inside both B and C.

Is the code right for the above requirement?
class A
{
D d;
};

class B : public A;
class C : public A;

Seems OK to me.
Now should all public and protected members in D be accessible to all
members of B and C?

No they shouldn't. There's no inheritance of B from D or of C from D, so
there is no automatic access to D's members from B or C.
Is this a good design?

That depends entirely on what you are trying to do. Good design doesn't
exist in the abstract.

john
 
V

velthuijsen

class A has its children/derived classes B and C. class D is supposed to be
contained inside both B and C.

Is the code right for the above requirement?
class A
{
D d;
};

class B : public A;
class C : public A;

Now should all public and protected members in D be accessible to all
members of B and C?
Is this a good design?
Thanks for your help!

Seeing that d is declared in the private block of A neither B nor C
has access to it.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top