当前位置:   article > 正文

类模板template <class T>_template

template

类模板template <class T>

类模板:

类模板是对一批仅仅成员数据类型不同的类的抽象,使用类模板可以极大地提高编程的效率。例如对于以下这个类:

typedef unsigned long Item;
class Stack
{
private:
	enum {MAX=10};//作用域内枚举,这里使用枚举的MAX只是一个符号名称,在作用域为整个类的代码中遇到时,将用10来代替
    //注意在此处使用const int MAX = 10;是不行的,因为声明类只是描述了对象的形式,并没有创建对象,因此在创建对象前将没有用于存储值得空间
    //另外一种在类中定义常量的方法——使用关键字static   例如:static const int MAX = 10;
    Item items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item & item);
    bool pop(Item & item);
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

上面这个类用来存储unsigned long类型的数值,当想存储double值或者string对象时,可涉及专门的Stack类,但是出了保存的对象类型不同之外,这两种Stack类的代码是相同的。然而与其编写一个泛型(即独立于类型的)栈,然后将具体的类型作为参数传递给这个类。这样就可以使用通用的代码生成存储不同类型的栈。C++的类模板为生成通用的类声明提供了一个较好的方法,即模板类。

类模板定义:

采用模板时,将使用模板定义替换Stack声明,使用模板成员函数替换Stack的成员函数。和模板函数一样,模板类以template <class Type>开头。template告诉编译器将要定义一个模板,这里的class并不意味着Type必须是一个类,而只是表明Type是一个通用的类型说明符,在使用模板时将用涉及的类型替换它。较新的C++允许在这种情况下使用不太容易混淆的关键字typename代替class,即template <typename Type>,其中泛型名Type可以更改

在模板定义中,可以使用泛型名来标识存储在栈中的类型,例如对于以上的Stack类,应将声明中所有的typedef标识符Item替换为Type。同样可以使用模板成员函数替换原来类的类方法,每个函数头都将以相同的模板声明打头:template <class Type>,另外类限定符要从Stack::改为Stack<Type>::

例如:

#ifndef STACK_H_
#define STACK_H_

template <class Type>
class Stack
{
private:
	enum {MAX=10};
    Type items[MAX];
    int top;
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    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>::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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

类模板使用:

仅在程序中包含模板并不能生成模板类,而必须请求实例化。为此需要声明一个类型为模板类的对象,方法是使用所需的具体类型替换泛型名。例如以下代码创建两个栈,一个用于存储int,另一个用于存储string对象:

Stack<int> kernels;
Stack<string> colonels;
  • 1
  • 2

注意必须显示地提供所需的类型,这与常规的函数模板是不同的,因为编译器可以根据函数的参数类型来确定要生成哪种函数:

template <class T>
void test(T t)
{
	cout << t << endl;
}
test(2);  //生成函数 void test(int)
test("two");  //生成函数 void test(const char *)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/576828
推荐阅读
相关标签
  

闽ICP备14008679号