inheritance over template class

G

Gaijinco

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!
 
T

terminator

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
 
D

Daniel Kraft

class Agenda : public List said:

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
 
J

James Kanze

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>.
 
S

Salt_Peter

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top