 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
David Curtis Guest
|
Posted: Sun Nov 28, 2004 2:08 am Post subject: Template function access quesiton |
|
|
Hi group. I have a template class that implements a linked list. I use this
template to manage four or five different object types. One of these object
types needs the list to be sorted. Because there are different object types
they won't sort by the same critieria. What I'm wondering is if a function
of the template class can access a member function of the type. Here's some
code:
template<class T>
void TLINKEDLIST<T>::Add(T *Tnew)
{
T *pItem = pHead;
while (Tnew->sort(pItem))
pItem = pItem->pNext;
}
This code is very incomplete but shows I'm trying to call sort function of
class T. Each object type will have it's own sort() function. Will this
work?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Nov 28, 2004 11:35 am Post subject: Re: Template function access quesiton |
|
|
"David Curtis" <dcurtis_0123 (AT) cogeco (DOT) ca> wrote...
| Quote: | Hi group. I have a template class that implements a linked list. I use
this
template to manage four or five different object types. One of these
object
types needs the list to be sorted. Because there are different object
types
they won't sort by the same critieria. What I'm wondering is if a function
of the template class can access a member function of the type. Here's
some
code:
template<class T
void TLINKEDLIST
{
T *pItem = pHead;
while (Tnew->sort(pItem))
pItem = pItem->pNext;
}
This code is very incomplete but shows I'm trying to call sort function of
class T. Each object type will have it's own sort() function. Will this
work?
|
I would rather do it through the mechanism known as "type traits" instead
of forcing your T class to have a non-member 'sort' function with the very
particular signature. How do you think it should behave if no sorting is
required?
I would probably implement a "find insertion spot" type of function, which
would always return the "last" for non-sorted lists and a proper location
for a sorted list (the element before which to insert).
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Terje Slettebų Guest
|
Posted: Sun Nov 28, 2004 6:09 pm Post subject: Re: Template function access quesiton |
|
|
"David Curtis" <dcurtis_0123 (AT) cogeco (DOT) ca> wrote
| Quote: | Hi group. I have a template class that implements a linked list.
|
One small thing: The usual term nowadays is "class template" (and "function
template"). Whenever I see "template class", I tend to stumble, because I
don't expect it, and it has generally ill-defined meaning (several).
| Quote: | I use this
template to manage four or five different object types. One of these
object
types needs the list to be sorted. Because there are different object
types
they won't sort by the same critieria. What I'm wondering is if a function
of the template class can access a member function of the type. Here's
some
code:
template<class T
void TLINKEDLIST
{
T *pItem = pHead;
while (Tnew->sort(pItem))
pItem = pItem->pNext;
}
This code is very incomplete but shows I'm trying to call sort function of
class T. Each object type will have it's own sort() function. Will this
work?
|
If you need the objects to be sorted at all times, how about using std::map,
instead (or std::multimap, if several may objects have the same key)? With
it, you can also pass in a predicate object, to determine the sorting of
each kind of object.
If you just need a plain list, there's std::list.
Regards,
Terje
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|