C++的模版类

1.不能将模版类的成员函数放在独立的实现文件中,必须将声明和定义放在一起。以前可以用export关键字,现在不行了。

如下面的例子。

#ifndef STACKP_H_
#define STACKP_H_
template <class Type>
{
private:
	enum{MAX=10};
	Type items[MAX];
	int top;
public:
	Stack();
	bool isempty();
	bool isfull();
	bool push(const Type & item);
	bool pop(Type & item);
};
template <class Type>
Stack<Type>::Stack()
{
	top=0;
}
template <class Type>
bool Stack<Type>::isempty()
{
	return top==0;
}
template <class Type>
bool Stack<Type>::push(const Type & item)
{
	if(top<MAX)
	{
		items[top++]=item;
		return true;
	}
	 return false;
}
template <class Type>
bool Stack<Type>::pop(Type & item)
{
	if(top>0)
	{
		item=item[--top];
		return true;
	}
	return false;
}
#endif
2.仅在程序中包含模板并不能生成模板类,而必须请求实例化。如下所示:

Stack<int> kernels;
Stack<int> colonels;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值