Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Another composition (http://www.velocityreviews.com/forums/t282478-another-composition.html)

fog 04-21-2004 06:39 AM

Another composition
 
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!




Claudio Puviani 04-21-2004 08:11 AM

Re: Another composition
 
"fog" <fog@turboweb.com> wrote
> 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



John Harrison 04-21-2004 08:21 AM

Re: Another composition
 

"fog" <fog@turboweb.com> wrote in message
news:LGohc.5548$_o3.190274@bgtnsc05-news.ops.worldnet.att.net...
> 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



velthuijsen 04-21-2004 09:45 AM

Re: Another composition
 
> 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.


All times are GMT. The time now is 12:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.