[prog] C++ Templates

Christine Bussman olearyck at SLU.EDU
Thu Jun 26 07:36:11 EST 2003


Please note that I'm just a beginner myself, so these are
just the things I would try myself, or things that look
wrong to me.  I'm also looking at a text I've used in the
past.

1.  I've always used template <class T> rather than
<typename T>, I don't know if this will make a difference

2.  I've also been taught to use :: rather than : for class
member functions.

3.  Your class definition makes insert a member function,
but your function 'implementation' doesn't.

4.  The other two functions probably need to be member
functions if they're accessing private data.

5.  The template <> before isfull just doesn't look right to
me.  I think you still want to use template <class T> here,
but this one I'm really not sure on.

If anyone who has more programming experience sees anywhere
I am wrong, I'd like to know also.

Let me know if any of this is unclear.

Hope this helps,
Christine

> #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
>
>
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming


More information about the Programming mailing list