implementing composition

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

    implementing composition

    1. How to make the code work?

    class CTest
    {
    public:
    int n;
    CTest(int v) : n(v) { }
    };

    class CContainer
    {
    int nn;
    private:
    vector<CTest> Array( nn, CTest(8) );
    public:
    CContainer(int v) : nn(v) {}
    };

    int main(void)
    {
    CContainer cc(50);
    ...
    }

    2. Compared to the above design of CContainer class, what benefits does the
    following one have?

    class CContainer
    {
    int nn;
    private:
    vector<CTest*> ptrToArray;
    public:
    CContainer(int v) : nn(v) {}

    };

    Thanks in advance!









  • Mike Wahler

    #2
    Re: implementing composition


    "ccs" <ccs@stopspammi ng.com> wrote in message
    news:shcyc.2819 $Di3.2198@bgtns c05-news.ops.worldn et.att.net...[color=blue]
    > 1. How to make the code work?
    >
    > class CTest
    > {
    > public:
    > int n;
    > CTest(int v) : n(v) { }
    > };
    >
    > class CContainer
    > {
    > int nn;
    > private:
    > vector<CTest> Array( nn, CTest(8) );
    > public:
    > CContainer(int v) : nn(v) {}
    > };
    >
    > int main(void)
    > {
    > CContainer cc(50);
    > ...
    > }[/color]

    Before you can get it to 'work', first you must get it to compile.
    The above does not.

    #include <vector>

    class CTest
    {
    public:
    int n;
    CTest(int v) : n(v) { }
    };

    class CContainer
    {
    int nn;
    private:
    std::vector<CTe st> Array;
    public:
    CContainer(int v) : nn(v), Array( v, CTest(8) ) {}
    };

    int main(void)
    {
    CContainer cc(50);
    return 0;
    }

    Now decide what it means to 'work', and make it do that.
    [color=blue]
    >
    > 2. Compared to the above design of CContainer class, what benefits does[/color]
    the[color=blue]
    > following one have?
    >
    > class CContainer
    > {
    > int nn;
    > private:
    > vector<CTest*> ptrToArray;[/color]

    Poor identifier name. This is not a pointer to an 'Array',
    but an 'Array' (actually a vector) of pointers (to type 'CTest').
    [color=blue]
    > public:
    > CContainer(int v) : nn(v) {}
    >
    > };[/color]

    You can't determine if it has any 'benefits' at all until
    you define your requirements. The first program implements
    a container of 'CTest' objects. The second one implements
    a container of pointers to 'CTest' objects. Which do you need?
    Why?

    Does not using a std::vector directly serve your needs rather
    than wrapping one in a custom type? Why?

    -Mike


    Comment

    • David Rubin

      #3
      Re: implementing composition

      "ccs" <ccs@stopspammi ng.com> wrote in message news:<shcyc.281 9$Di3.2198@bgtn sc05-news.ops.worldn et.att.net>...[color=blue]
      > 1. How to make the code work?[/color]
      [color=blue]
      > class CTest
      > {
      > public:
      > int n;
      > CTest(int v) : n(v) { }
      > };[/color]
      [color=blue]
      > class CContainer
      > {
      > int nn;
      > private:
      > vector<CTest> Array( nn, CTest(8) );[/color]

      vector<CTest> Array;
      [color=blue]
      > public:
      > CContainer(int v) : nn(v) {}[/color]

      CContainer(int v);[color=blue]
      > };[/color]

      inline
      CContainer::CCo ntainer(int v)
      : nn(v), Array(nn, CTest(8))
      {
      }
      [color=blue]
      > int main(void)
      > {
      > CContainer cc(50);
      > ...
      > }[/color]
      [color=blue]
      > 2. Compared to the above design of CContainer class, what benefits does the
      > following one have?[/color]
      [color=blue]
      > class CContainer
      > {
      > int nn;
      > private:
      > vector<CTest*> ptrToArray;
      > public:
      > CContainer(int v) : nn(v) {}
      >
      > };[/color]

      A vector of pointer-to-CTest allows you to call polymorphic functions
      of CTest in CContainer (or elsewhere, given adequate exposure of
      Array). However, CContainer most likely will require clients to
      guarantee the lifetime of Array elements, and clients are responsible
      for destroying Array elements. The first design stores copies of CTest
      instances (requiring a valid CTest copy constructor). /david

      Comment

      Working...