Question regarding a container of objects

Discussion in 'C++' started by tomix, Oct 23, 2006.

  1. tomix

    tomix Guest

    Hi,

    assuming i have a container of objects, for example:

    Course contains a list of CourseOffer which itself is a container of
    duration objects

    And assuming CourseOffer only contained by Course

    i wonder what do you think is a better way to implement it


    Course* c=GetCourse();
    CourseOffer* cf=new CourseOffer(.....);
    cf->AddDuration(10,12);
    cf->AddDuration(16,20);
    c->Add(cf);
    :
    :

    or

    Course* c=GetCourse();
    CourseOffer* cf=c.CreateCourseOffer(....);
    cf->AddDuration(10,12);
    cf->AddDuration(16,20);

    I'll go for the second since course control the creation and lifetime
    of a course offer

    Thanks in advance.
    tomix, Oct 23, 2006
    #1
    1. Advertising

  2. tomix wrote:
    > assuming i have a container of objects, for example:
    >
    > Course contains a list of CourseOffer which itself is a container of
    > duration objects
    >
    > And assuming CourseOffer only contained by Course
    >
    > i wonder what do you think is a better way to implement it
    >
    >
    > Course* c=GetCourse();
    > CourseOffer* cf=new CourseOffer(.....);
    > cf->AddDuration(10,12);
    > cf->AddDuration(16,20);
    > c->Add(cf);
    >>
    >>

    >
    > or
    >
    > Course* c=GetCourse();
    > CourseOffer* cf=c.CreateCourseOffer(....);
    > cf->AddDuration(10,12);
    > cf->AddDuration(16,20);
    >
    > I'll go for the second since course control the creation and lifetime
    > of a course offer
    >
    > Thanks in advance.


    If 'CourseOffer' has to be public, then yes, the latter choice is better.
    But if 'CourseOffer' is just an implementation detail of 'Course', then
    you could simply add the proper interface to 'Course'...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 23, 2006
    #2
    1. Advertising

  3. tomix

    Daniel T. Guest

    "tomix" <> wrote:

    > Hi,
    >
    > assuming i have a container of objects, for example:
    >
    > Course contains a list of CourseOffer which itself is a container of
    > duration objects
    >
    > And assuming CourseOffer only contained by Course
    >
    > i wonder what do you think is a better way to implement it
    >
    >
    > Course* c=GetCourse();
    > CourseOffer* cf=new CourseOffer(.....);
    > cf->AddDuration(10,12);
    > cf->AddDuration(16,20);
    > c->Add(cf);
    > :
    > :
    >
    > or
    >
    > Course* c=GetCourse();
    > CourseOffer* cf=c.CreateCourseOffer(....);
    > cf->AddDuration(10,12);
    > cf->AddDuration(16,20);
    >
    > I'll go for the second since course control the creation and lifetime
    > of a course offer
    >
    > Thanks in advance.


    I generally go for either completely private, or generally extensible.
    This means either option one above (which allows us to provide
    sub-classes of CourseOffer to a Course,) or make it so the user of
    Course need know nothing at all about the CourseOffer class.

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Oct 23, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vivi Orunitia
    Replies:
    11
    Views:
    4,473
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    521
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,896
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    798
    Daniel T.
    Feb 16, 2006
  5. Derek Basch
    Replies:
    4
    Views:
    184
    -berlin.de
    Aug 16, 2006
Loading...

Share This Page