c++ 模板类 后面每个方法前的template <class Type>有什么意义 对每个方法有什么影响?

// stacktp.h -- a stack template
#ifndef STACKTP_H_
#define STACKTP_H_
template <class Type>
class Stack
{
private:
enum {MAX = 10}; // constant specific to class
Type items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty();
bool isfull();
bool push(const Type & item); // add item to stack
bool pop(Type & item); // pop top into 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>::isfull()
{
return top == MAX;
}

template <class Type>
bool Stack<Type>::push(const Type & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}

template <class Type>
bool Stack<Type>::pop(Type & item)
{
if (top > 0)
{
item = items[--top];
return true;
}
else
return false;
}

#endif

c++集合了过程式语言,通用语言,面向对象语言的众多特点。模板是通用语言的特性,模板又叫参数化类型(parametrized types)。

模板的定义。以下是模板定义的一般格式:

  template <class any>//class 也可以换成typename,后者是新来的。

  void swap(any &a,any &b)

  {

  ......

  }

利用模板机制可以显著减少冗余信息,能大幅度地节约程序代码,进一步提高面向对象程序的可重用性和维护性。

在使用过程中,注意的问题:

(1)在template语句与函数模板定义语句之间不允许有别的语句。

template<class T>

int i; // 错误,不允许插入别的语句

T max(T x,T y)

(2)模板函数类似于重载函数,只不过它更严格一些。函数被重载的时候,在每个函数体内可以执行不同的动作。但同一个函数模板实例化后的所有模板函数都必须执行相同的动作。例如,下面重载函数就不能用模板函数代替,因为它们所执行的动作是不同的。

void outdate(int i)

{ cout<<i; }

void outdata(double d)

{ cout<<"d="<<d>>endl; }

(3)在函数模板中允许使用多个类型参数。但是应当注意template定义部分的每个类型参数前必须有关键字class(typename)。

#inculde<iostream.h>

template<typename type1,ypename type2>

void myfunc(type1 x,type2 y)

{ cout<<x<<' '<<y<<endl; }

main()

{

myfunc(10,"hao");

myfunc(0.123,10L);

return 0;

}

(4)函数模板的异常处理。函数模板有一个特点,虽然函数模板中德类型参数T可以实例化为各种类型,但是采用类型参数T的每个参数必须实例化成完全相同的类型。模板类型不具有隐式的类型转换。如果不注意这一点,可能产生错误。

template<class T>

T max(T x,T y)

{ return (x>y)?x:y; }

void fun(int i,char c)

{

max(i,i); // 正确,调用max(int,int)
温馨提示:答案为网友推荐,仅供参考
相似回答