Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14837
  • 博文数量: 5
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-25 16:03
个人简介

雄鹰可以飞的很晚,一定要飞的很高。

文章分类
文章存档

2016年(5)

我的朋友
最近访客

分类: C/C++

2016-08-30 14:30:11

1.定义一个模板类:

点击(此处)折叠或打开

  1. template<class 模板参数表>

  2. class 类名
  3. {
  4.      // 类定义 。。。。。
  5. };
说明:template 是声明类模板的关键字,表示声明一个模板。模板参数可以是一个,或者多个,可以是类型参数,也可以是非类型参数。
       类型参数由关键字class或typename及其后面的标识符构成,非类型参数有一个普通参数构成,代表模板定义一个常量。

例:

点击(此处)折叠或打开

  1. template<class type, int width>
  2. //type为类型参数,width为非类型参数
  3. class Graphics;
注意:(1).如果在全局变量中声明了与模板参数同名的变量,则该变量被隐藏掉。
          (2).  模板参数名不能被当作类模板定义中类成员的名字。
           (3)同一个模板参数名在模板参数表中只能出现一次。
         (4)在不同的类模板参数或声明中,模板参数名可以被重复使用。

点击(此处)折叠或打开

  1. typedef string type;

  2. template<class type,int width>

  3. class Graphics

  4. {

  5. type node;//node不是string类型

  6. typedef double type;//错误:成员名不能与模板参数type同名

  7. };

  8. template<class type,class type>//错误:重复使用名为type的参数

  9. class Rect;

  10. template<class type> //参数名”type”在不同模板间可以重复使用

  11. class Round;

(5)在类模板的前向声明和定义中,模板参数的名字可以不同。

点击(此处)折叠或打开

  1. // 所有三个 Image 声明都引用同一个类模板的声明

  2. template <class T> class Image;

  3. template <class U> class Image;

  4. // 模板的真正定义

  5. template <class Type>

  6. class Image { //模板定义中只能引用名字”Type”,不能引用名字”T”和”U” };


       
(6)类模板参数可以有缺省参数。给参数提供缺省实参的顺序是先右后左。

点击(此处)折叠或打开

  1. template <class type, int size = 1024>

  2. class Image;

  3. template <class type=double, int size >

  4. class Image;
(7)类模板名可以被用作一个类型指示符。当一个类模板名被用作另一个模板定义中的类型指示符时,必须指定完整的实参表

点击(此处)折叠或打开

  1. template<class type>

  2. class Graphics

  3. {

  4. Graphics *next;//在类模板自己的定义中不需指定完整模板参数表

  5. };

  6. template <calss type>

  7. void show(Graphics<type> &g)

  8. {

  9. Graphics<type> *pg=&g;//必须指定完整的模板参数表

  10. }

2.类模板实例


 1.支持不同数据类型的函数重载:

点击(此处)折叠或打开

  1. #include <iostream>
  2. using namespace std;

  3. int square (int x)
  4. {
  5.   return x * x;
  6. };

  7. float square (float x)
  8. {
  9.   return x * x;
  10. };

  11. double square (double x)
  12. {
  13.   return x * x;
  14. };

  15. main()
  16. {
  17.    int i, ii;
  18.    float x, xx;
  19.    double y, yy;

  20.    i = 2;
  21.    x = 2.2;
  22.    y = 2.2;

  23.    ii = square(i);
  24.    cout << i << ": " << ii << endl;

  25.    xx = square(x);
  26.    cout << x << ": " << xx << endl;

  27.    yy = square(y);
  28.    cout << y << ": " << yy << endl;
  29. }

2.支持所有数据类型的函数模板

点击(此处)折叠或打开

  1. #include <iostream>
  2. using namespace std;

  3. template <class T>
  4. inline T square(T x)
  5. {
  6.    T result;
  7.    result = x * x;
  8.    return result;
  9. };



  10. main()
  11. {
  12.    int i, ii;
  13.    float x, xx;
  14.    double y, yy;

  15.    i = 2;
  16.    x = 2.2;
  17.    y = 2.2;

  18.    ii = square<int>(i);
  19.    cout << i << ": " << ii << endl;

  20.    xx = square<float>(x);
  21.    cout << x << ": " << xx << endl;

  22.    // Explicit use of template
  23.    yy = square<double>(y);// 显式使用模板
  24.    cout << y << ": " << yy << endl;

  25.    yy = square(y);//隐含的方式使用模板
  26.    cout << y << ": " << yy << endl;
  27. }

注明:模板的关键字可以用class或者typename.

  • template
  • template

两者表达的意思是一样的,但是我更喜欢使用后者。

  • 可以采用两种方式使用模板函数square(value) or square(value).
  • 在模板函数的定义中,T代表数据类型。
  • 模板的声明和定义必须在同一个文件中,如头文件中。
  • C语言的宏定义也可以实现函数模板的功能,#define square(x) (x * x) 
    但是宏没有类型检查,函数模板有类型检查。







  • 阅读(781) | 评论(0) | 转发(0) |
    0

    上一篇:时间编程

    下一篇:没有了

    给主人留下些什么吧!~~