Stack.h
//
// Created by JAN on 2022/1/26.
//
#ifndef C___STACK_H
#define C___STACK_H
template <class T>
class Stack
{
private:
enum { MAX = 10};
T items[MAX];
int now;
public:
Stack() : now(0) {}
bool push(T & t);
bool pop();
bool empyt() const;
const T& top() const;
};
template <class T>
bool Stack<T>::push(T &t)
{
if(now < MAX){
items[now++] = t;
return true;
}else
return false;
}
template<class T>
bool Stack<T>::empyt() const
{
if(now == 0)
return true;
else
return false;
}
template<class T>
const T &Stack<T>::top() const
{
//int temp = now-1;
return items[now-1];
}
template<class T>
bool Stack<T>::pop()
{
if(now > 0){
now--;
return true;
}else
return false;
}
#endif //C___STACK_H
模板的具体实现叫做实例化或者具体化。仅仅在头文件中写出模板的定义不叫具体化。由于模板不是函数,他们不能单独编译。模板必须和特定的模板实例化请求一起使用。为此,最简单的方法是将所有的模板信息放在一个头文件中,并在要使用这些模板的文件中包含该头文件。