Another composition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fog

    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

    #2
    Re: Another composition

    "fog" <fog@turboweb.c om> wrote[color=blue]
    > 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?[/color]

    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


    Comment

    • John Harrison

      #3
      Re: Another composition


      "fog" <fog@turboweb.c om> wrote in message
      news:LGohc.5548 $_o3.190274@bgt nsc05-news.ops.worldn et.att.net...[color=blue]
      > class A has its children/derived classes B and C. class D is supposed to[/color]
      be[color=blue]
      > 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;[/color]

      Seems OK to me.
      [color=blue]
      >
      > Now should all public and protected members in D be accessible to all
      > members of B and C?[/color]

      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.
      [color=blue]
      > Is this a good design?[/color]

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

      john


      Comment

      • velthuijsen

        #4
        Re: Another composition

        > class A has its children/derived classes B and C. class D is supposed to be[color=blue]
        > 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![/color]

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

        Comment

        Working...