您的位置:首页 > 编程语言 > C语言/C++

C++模板学习

2013-09-25 00:24 316 查看
一、什么是模板?

  模板就是实现代码重用机制的一种工具,它可以实现类型参数化,即把类型定义为参数,从而实现了真正的代码可重用性。模板可以分为两类,一个是模板函数,一个是模板类。

二、c++中的模板和模板类

  What's better than having several classes that do the same thing to different datatypes? One class that lets you choose which datatype it acts on.

  模板提供了这样一种途径来定义类的行为(无需事先了解哪种数据类型将会被用于类的操作处理上),本质上说就是大家所熟知的泛型编程。

the basic syntax for declaring a template class is as follows:

template <class a_type>
class a_class
{
......
};


在定义模板类中的变量时,如下行所示:

a_type a_var;


我们需要将模板类的成员函数定义为模板函数:

template <class a_type>
a_type a_class<a_type>::a_function()
{
......
}


when declaring an instance of a template class, the syntax is as follows:

类模板的实例化对象就叫做特化模板类,模板参数不再使用抽象的类型,而是直接使用具体的类型来实例化该模板类。

a_class<int> an_example_class;


下面给出一个模板样例来作为这一小节的结束

template <class T>
class calc
{
public:
T multiply(T x, T y);
T add(T x, T y);
};

template <class T>
T calc<T>::multiply(T x, T y)
{
return x*y;
}

template <class T>
T calc<T>::add(T x, T y)
{
return x+y;
}


now, when you instantiate an object of class calc you can choose which datatype the class will handle.

calc<double> a_calc_class;


三、模板函数

  在上一节中我们已经对模板函数有了一些了解,接下来再较详细介绍一下。模板函数实例化如下,我们在实际应用中可以用任意类型来实例化该模板函数

#include <stdio.h>

template<class T>
T cosnt &max(T const &a, T const &b)
{
return a < b ? b : a;
}

int main()
{
printf("The max value is %d\n",max(4,5));    //max<int>
printf("The max value is %f\n",max(4.1,5.1));    //max<float>
return 0;
}


  和普通函数重载一样,模板函数也可以实现函数重载的功能,如下所示:

///////////////////////////////////////////////////////////
//  Copyright (c) 2013, ShangHai xxxxx Inc.
//
//    FileName:   overloadTemplateFunc.cpp
//
//    Description:
//
//    Created:    Wed Sep 25 14:41:21 2013
//    Revision:   Revision: 1.0
//    Compiler:   g++
//
///////////////////////////////////////////////////////////
#include <stdio.h>

int const &max(int const &a, int const &b)
{
printf("int const &max(int const &a, int const &b) is called.\n");
return a < b ? b : a;
}

template <class T>
T const &max(T const &a, T const &b)
{
printf("T const &max(T const &a, T const &b) is called.\n");
return a < b ? b : a;
}

template <class T>
T const &max(T const &a, T const &b, T const &c)
{
printf("T const &max(T const &a, T const &b, T const &c) is called.\n");
return max(max(a,b),c);
}

class MyClass
{
public:
MyClass(int value):m_value(value)
{
}
bool operator<(const MyClass &other) const
{
printf("operator < is called.\n");
return m_value < other.m_value;
}
int getValue() const
{
return m_value;
}

private:
int m_value;
};

int main()
{
printf("--------------------------------\n");
max(5,10,15);
printf("--------------------------------\n");
max(5,10);
max<>(5,10);
max('a','b');
max('a',10);
printf("--------------------------------\n");
MyClass class1(10),class2(100);
max(class1,class2);
printf("--------------------------------\n");

getchar();
return 0;
}

[yangtze@contex201 ~]$ ./overloadTemplateFunc
--------------------------------
T const &max(T const &a, T const &b, T const &c) is called.
int const &max(int const &a, int const &b) is called.
int const &max(int const &a, int const &b) is called.
--------------------------------
int const &max(int const &a, int const &b) is called.
T const &max(T const &a, T const &b) is called.
T const &max(T const &a, T const &b) is called.
int const &max(int const &a, int const &b) is called.
--------------------------------
T const &max(T const &a, T const &b) is called.
operator < is called.
--------------------------------

  结合运行结果分析,可以总结以下几点:

1)编译器在进行函数重载的推演时,首选非模板类型函数;

max(5,10)的运行结果是:

  int const &max(int const &a, int const &b) is called.

max(5,10,15)的运行结果是:

  T const &max(T const &a, T const &b, T const &c) is called.
  int const &max(int const &a, int const &b) is called.
  int const &max(int const &a, int const &b) is called.

2)非模板和模板可以同名,在调用时可以一起进行函数重载的推演(显而易见);

3)通过max<>方式通知编译器,在推演时只考虑函数模板;

max<>(5,10)的运行结果是:

  T const &max(T const &a, T const &b) is called.

4)模板函数实例化过程中,并不会自动完成任何形式的类型转换,而普通函数max通过类型自动转换((int)'a',10)可以覆盖该调用,使得编译器选择了普通函数。

max('a',10)的运行结果是:

  int const &max(int const &a, int const &b) is called.

四、模板类特化

  //TODO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: