من اين كلاس template رو نوشتم ولي اگه به تايع main توجه كنيد متوجه مي شويد كه cout از چپ به راست عمل ميكند.
کد:
#ifndef myList_H
#define myList_H
#include <stdlib.h>
template <typename T>class myList
{
public:
myList(){length = 0;}
void add(T );
T pop();
int count();
~myList(){delete []LMembers;}
private:
T *LMembers;
int length;
};
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//Class Functions:
template <typename T>void myList<T>::add(T item)
{
if(length == 0)
{
LMembers = new T[1];
LMembers[0] = item;
length = 1;
}
else
{
T *temp;
temp = new T[length + 1];
for (int i = 0; i < length; i++)
temp[i] = LMembers[i];
temp[length] = item;
delete []LMembers;
LMembers = new T [length + 1];
for(int i = 0; i < (length + 1); i++ )
LMembers[i] = temp[i];
length++;
}
}
template<typename T>int myList<T>::count()
{
return length;
}
template <typename T> T myList<T>::pop()
{
if(length==1)
{
return LMembers[0];
}
else
{
T tempMember = LMembers[0];
T *temp;
temp = new T[length-1];
for (int i = 0; i < length - 1; i++)
temp[i] = LMembers[i + 1];
delete []LMembers;
LMembers = new T[length-1];
for (int i = 0; i < length - 1; i++)
LMembers[i] = temp[i];
length--;
return tempMember;
}
}
#endif
#include <iostream>
#include "test.h"
using namespace std;
main()
{
myList <int> test;
test.add(3);
test.add(4);
test.add(5);
test.add(6);
cout << "count == "<< test.count() << " \n pop1 == " <<test.pop() << " \n count 2 == " << test.count() << " " << test.pop() << " " << test.count() << endl;
cin.get();
cin.get();
}