C++类模版学习笔记

C++在发展的后期增加了模板(template )的功能,提供了解决这类问题的途径。

可以声明一个通用的类模板,它可以有一个或多个虚拟的类型参数,如对多种类型比较大小的类可以综合写出以下的类模板:

/****************************************
* File Name: tmp.cpp
* Author: sky0917
* Created Time: 2014年04月19日 21:22:25
****************************************/
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

struct node{
    int x, y;

    node(){}
    node(int a, int b):x(a),y(b){}

    bool operator > (node &a)const{
        return x > a.x && y > a.y;
    }
    bool operator < (node &a)const{
        return x < a.x && y < a.y;    
    }    
    friend ostream& operator << (ostream &os, node &t){
        os<<t.x << ' ' << t.y;    
    }
};

template <class numtype>  // 声明一个模版(用来比较每种常用类型的大小),虚拟类型名为numtype
class Cmp{ // 类模版名为 Cmp
    public:
        Cmp(numtype a, numtype b){
            x = a, y = b;
        }
        numtype max(){
            if (x > y) return x;
            else return y;
        }
        numtype min(){
            return x<y?x:y;
        }
    private:
        numtype x, y;
};

void test(){
    Cmp <int> cmp(4, 7);
    cout<<"int cmp"<<endl;
    cout<<"max = "<<cmp.max()<<endl;
    cout<<"min = "<<cmp.min()<<endl;
    
    Cmp <double> cmp2(3.2, 2.3);
    cout<<"double cmp2"<<endl;    
    cout<<"max = "<<cmp2.max()<<endl;
    cout<<"min = "<<cmp2.min()<<endl;    

    node a(1,2);
    node b(3,4);
    Cmp <node> cmp3(a, b);
    node c = cmp3.max();
    node d = cmp3.min();
    cout << "max = " << c << endl;
    cout << "min = " << d << endl;    
}

int main(){
    test();        
    return 0;
}

利用类模板可以建立含各种数据类型的类。在声明了一个类模板后,怎样使用它?怎样使它变成一个实际的类?
先回顾一下用类来定义对象的方法:

   Compare_int cmp1(4,7);  // Compare_int是已声明的类
用类模板定义对象的方法与此相似,但是不能直接写成
   Cmp cmp(4,7); // Cmp是类模板名
   Cmp是类模板名,而不是一个具体的类,类模板体中的类型numtype并不是一个实际的类型,只是一个虚拟的类型,无法用它去定义对象。
必须用实际类型名去取代虚拟的类型,具体的做法是:
   Cmp <int> cmp(4,7);
即在类模板名之后在尖括号内指定实际的类型名,在进行编译时,编译系统就用int取代类模板中的类型参数numtype,这样就把类模板具体化了,

或者说实例化了。这时Cmp<int>就相当于前面介绍的Compare_int类。

 

还有一个问题要说明: 上面列出的类模板中的成员函数是在类模板内定义的。

如果改为在类模板外定义,不能用一般定义类成员函数的形式:
numtype Cmp::max( ) {…} //不能这样定义类模板中的成员函数
而应当写成类模板的形式:

template <class numtype>
numtype Cmp<numtype>::max( )
{
    return (x>y)?x:y;
}

归纳以上的介绍,可以这样声明和使用类模板:

1. 先写出一个实际的类。由于其语义明确,含义清楚,一般不会出错。

2. 将此类中准备改变的类型名(如int要改变为float或char)改用一个自己指定的虚拟类型名(如上例中的numtype)。

3. 在类声明前面加入一行,格式为

template <class 虚拟类型参数>,如
template <class numtype> //注意本行末尾无分号
class Compare
{…}; //类体

4. 用类模板定义对象时用以下形式:
   类模板名<实际类型名> 对象名;
   类模板名<实际类型名> 对象名(实参表列);

   Compare<int> cmp;
   Compare<int> cmp(3,7);

5. 如果在类模板外定义成员函数,应写成类模板形式:
   template <class 虚拟类型参数>
   函数类型 类模板名<虚拟类型参数>::成员函数名(函数形参表列) {…}


关于类模板的几点说明:

1. 类模板的类型参数可以有一个或多个,每个类型前面都必须加class,如
template <class T1,class T2>
class someclass
{…};
在定义对象时分别代入实际的类型名,如
someclass <int, double> obj;

2. 和使用类一样,使用类模板时要注意其作用域,只能在其有效作用域内用它定义对象。

3. 模板可以有层次,一个类模板可以作为基类,派生出派生模板类。

 

原文地址:https://www.cnblogs.com/sky0917/p/3675666.html