Topic: Template Question
Author: gonzo@st-elsewhere.Eng.Sun.COM (James Todd)
Date: 11 Dec 1992 00:43:24 GMT Raw View
Hi -
I'm interested in using templates to build a linked list of classes.
As of now I have the following:
template<class T> class node {
private:
node<T>* next;
const T item;
public:
node(const T& i) : next(0), item(i) {}
~node() {}
node<T>* getnext() const { return(next); }
void putnext(node<T>* ptr) { next = ptr; }
const T& get() const { return(item); }
};
template<class T> class list {
private:
node<T>* head;
node<T>* current;
node<T>* getprevious() const;
node<T>* getnext() const;
public:
list() : head(0), current(0) {}
~list() { clear(); }
unsigned int count() const;
inline void first() { set(); }
inline void last() { set(count()); }
inline void previous() { current = getprevious(); }
inline void next() { current = getnext(); }
void set(unsigned int = 0);
node<T>* get(unsigned int = 0) const;
void put(const T&);
void clear();
};
The problem is that I need a "list<T>::find(const T&)" function. This
function is unique for every class T (ie. T has it' own *find* member
function).
I am thinking/reaching/grasping along the lines of the following:
template<class T> void list<T>::find(const T& t)
{
.
.
//call T's find member function.(can I do this???)
<T>::find(t);
.
.
}
void T::find(const T& t)
{
.
.
//append every found record to list.(can I do this???)
while (whatever)
list<T>::put(t);
.
.
}
Can I do the above?
If not, any suggestions?
Any suggestions as to a better/correct way to accomplish this task?
Thanks Much,
- James