类模板对象的引用?
类模板在C++很重要吗?常用吗?
#include<iostream.h>//一处错误
#include<conio.h>
template<class SType>
class stack
{
public:
stack(int size);
~stack()
{
delete []stck;
}
void push(SType i);
SType pop();
private:
int tos,length;
SType *stck;
};
template<class SType>
stack<SType>::stack(int size)
{
stck<SType>::push(SType i)
{cout<<"can not allocate stack memory!"<<endl;
exit(1);
}
length=size;
tos=0;
}
template<class SType>
void stack<SType>::push(SType i)
{if(tos==length)
{cout<<"stack is full.\n";
return;
}
sck[tos]=i;
tos++;
}
template<class SType>
SType stack<SType>::pop()
{if(tos==0)
{cout<<"stack nuderflow.\n";
return 0;
}
tos--;
return stck[tos];
}
template<class SType>
void myfun(stack<SType>&sta)//这个SYype不能换成其它的类型吗?
{cout<<sta.pop()<<endl;
}
void main()
{
stack<int>s(10);
for(int i=14;i<19;i++)
s.push(i);
for(int i=0;i<5;i++)
myfun(s);
cout<<endl;
getch();
}