inheritance over template class

Discussion in 'C++' started by Gaijinco, May 26, 2007.

  1. Gaijinco

    Gaijinco Guest

    If I have a template class like:

    template <typename T>
    class List
    {
    // something here
    };

    and another class

    class Contact
    {
    // something here
    };

    What makes more sense:

    class Agenda : public List<Contact>
    {

    };

    or should I inherit from the template class like

    template <typename T>
    class Agenda : public List<T>
    {

    };

    Thanks!
     
    Gaijinco, May 26, 2007
    #1
    1. Advertising

  2. Gaijinco

    terminator Guest

    On May 26, 5:44 pm, Gaijinco <> wrote:
    > If I have a template class like:
    >
    > template <typename T>
    > class List
    > {
    > // something here
    >
    > };
    >
    > and another class
    >
    > class Contact
    > {
    > // something here
    >
    > };
    >
    > What makes more sense:
    >
    > class Agenda : public List<Contact>
    > {
    >
    > };
    >
    > or should I inherit from the template class like
    >
    > template <typename T>
    > class Agenda : public List<T>
    > {
    >
    > };
    >
    > Thanks!


    It depends on how you are going to use Agenda; If it is intended to
    simply work with Contact then the former solution is enough, But if a
    similar use of some class(other than Contact) is intended then you`d
    better do the later.

    regards,
    FM
     
    terminator, May 26, 2007
    #2
    1. Advertising

  3. Gaijinco

    Daniel Kraft Guest

    > class Agenda : public List<Contact>
    > {
    >
    > };


    This one probably makes sense if you want Agenda to be simply a List of
    Contact's.

    > template <typename T>
    > class Agenda : public List<T>
    > {
    >
    > };


    Here you define Agenda so it can be an (probably) extended List of every
    things you want; this could make sense, too -- depends on what you want
    to do.

    Yours,
    Daniel

    --
    Got two Dear-Daniel-Instant Messages
    by MSN, associate ICQ with stress --
    so please use good, old E-MAIL!
     
    Daniel Kraft, May 26, 2007
    #3
  4. Gaijinco

    James Kanze Guest

    On May 26, 3:44 pm, Gaijinco <> wrote:
    > If I have a template class like:


    > template <typename T>
    > class List
    > {
    > // something here
    > };


    > and another class


    > class Contact
    > {
    > // something here
    > };


    > What makes more sense:


    > class Agenda : public List<Contact>
    > {
    > };


    > or should I inherit from the template class like


    > template <typename T>
    > class Agenda : public List<T>
    > {
    > };


    It depends on what you want to do. Judging from the names,
    neither really makes sense: you probably want a non-template
    Agenda which contains a List<Contact>.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 27, 2007
    #4
  5. Gaijinco

    Salt_Peter Guest

    On May 26, 9:44 am, Gaijinco <> wrote:
    > If I have a template class like:
    >
    > template <typename T>
    > class List
    > {
    > // something here
    >
    > };
    >
    > and another class
    >
    > class Contact
    > {
    > // something here
    >
    > };
    >
    > What makes more sense:
    >
    > class Agenda : public List<Contact>
    > {
    >
    > };
    >
    > or should I inherit from the template class like
    >
    > template <typename T>
    > class Agenda : public List<T>
    > {
    >
    > };
    >
    > Thanks!


    I fail to see an Agenda as only a List of Contacts.
    It might also keep a schedule, appointments, notes, etc.
    I see an Agenda which has_a list of contacts and perhaps more than one
    list.
    So even class Agenda : private List< Contact > { } doesn't make sense.
    How about a collection of ordered contacts?
    Perhaps ordered in more than one way ( by nickname, or by firstname
    and then lastname).

    #include <set>
    #include <list>

    template< typename C,
    typename S,
    typename A,
    typename N >
    class Agenda
    {
    std::set< C > m_contacts;
    std::set< S > m_schedule;
    std::set< A > m_appointments;
    std::list< N > m_notes;
    ...
    };

    Since the std::set relies on the default predicate std::less, each
    respective type needs only supply the appropriate op< to order each
    collection automatically at insertion time. If an element needs
    modification, remove it, modify it and reinsert it.

    You might consider only having one Contact type and provide optional
    predicates for the client of your program to choose from.
     
    Salt_Peter, May 27, 2007
    #5
  6. Gaijinco

    Gaijinco Guest

    Thanks to all of you for your suggestions!
     
    Gaijinco, May 29, 2007
    #6
    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. christopher diggins
    Replies:
    16
    Views:
    756
    Pete Becker
    May 4, 2005
  2. Replies:
    1
    Views:
    2,113
    Gianni Mariani
    Jun 8, 2007
  3. nguillot
    Replies:
    5
    Views:
    532
  4. DeMarcus
    Replies:
    4
    Views:
    568
    Michael Doubez
    Apr 24, 2010
  5. A L
    Replies:
    1
    Views:
    511
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page