[prog] C++ Templates

Sue Stones suzo at bigpond.net.au
Thu Jun 26 22:07:45 EST 2003


I am trying to figgure out how to create a template class.  The class 
declaration is fine, but how do I write the definitions of the member 
functions??? 

I have 3 books here in front of me, and I can't understand any of them! Can 
anyone explain how I amsupposed to do this!

Here is the code of just one of the things that I have tried, it just happens 
to be the stage it is currently in as I keeps changing things trying to find 
the right syntax.

thanks

sue

#include <iostream>

template <typename T>
class Ordlist
{
private:
        T * info;
        int count;
public:
        Ordlist(T size = 20); // constructor
        ~Ordlist(void);            // destructor
        int insert(T item);  // assuming the return type is a boolean
        int isfull( void);
        void display ( void);
};      // class

template <typename T>
Ordlist<T size> : count(size)
{
        info = new T[size * sizeof(T)];
        for(int i=0; i<size; i++)
                info[i] = '\0'; // initialise array
} // constructor


template <typename T>
int insert(T item)
{
        T * tp = item;
        T * temp;
        for(int i=count; i > 0; i--)
                if (info[i] < tp)
                {
                        temp = info[i];
                        info[i] = tp;
                        tp = temp;
                }//if
        count--;
} // insert

template <>
int isfull(void)
{
        if (count) return 1;
        return 0;
} // isfull


void display ( void)
{
        for(int i=count; i>0; i--)
                std::cout << info[i];
}//dislplay





More information about the Programming mailing list