类模版和函数模版需要注意的

函数模版要注意的地方见注释,代码如下:

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

/*
 * 函数模版的格式为: template<class T> 函数名(函数参数);
 * 模版函数的实现要和声明放在一起,如果不放在一起,在实现的时候要重新声明template<class T> 
 */
template<class T>
void add(T a, T b)
{
	T temp = 0;
	temp = a + b;
	cout << temp << endl;
}

template<class T>
void sub(T a, T b)
{
	T temp = 0;
	temp = a-b;
	cout << temp << endl;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int num1 = 1;
	int num2 = 2;
	double db1 = 22.33;
	double db2 = 33.55;
	add(num1, num2);
	add(db1, db2);
	sub(db1, db2);
	system("pause");
	return 0;
}

 

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

/*
 * 函数模版的格式为: template<class T> 函数名(函数参数);
 * 模版函数的实现要和声明放在一起
 * 如果不放在一起,在实现的时候要重新声明template<class T>
 * 之所以这样的原因是,编译的过程中编译器遇到template<class T>
 * 之后,才知道下边的类或者函数是一个特殊的函数模版或者类模版
 *
 */
template<class T>
void add(T a, T b);

template<class T>
void sub(T a, T b);

int _tmain(int argc, _TCHAR* argv[])
{
	int num1 = 1;
	int num2 = 2;
	double db1 = 22.33;
	double db2 = 33.55;
	add(num1, num2);
	add(db1, db2);
	sub(db1, db2);
	system("pause");
	return 0;
}
template<class T>
void sub(T a, T b)
{
	T temp = 0;
	temp = a-b;
	cout << temp << endl;
}

template<class T>
void add(T a, T b)
{
	T temp = 0;
	temp = a + b;
	cout << temp << endl;
}


对于类模版,成员函数的声明必须在.h文件中完成,.cpp中不写code。否则编译器会报错:无法解析的外部符号 "public: void __thiscall Student<int,int>::add(int,int)" (?add@?$Student@HH@@QAEXHH@Z),该符号在函数 _wmain 中被引用

另外对于类成员函数的定义要跟随其声明,如果在类外进行函数的定义需要添加模版的声明。见如下程序:

#pragma once
template<class T1, class T2>
class Student
{
 /*在声明的同时进行定义,在这里可以看出,编译器在编译的时候,
 先对成员变量进行编译,然后在编译成员函数*/
public:
 Student(void){m_TNum1 = 0;
 m_TNum2 = 0;
 };      
 void add(T1 t_num1, T2 t_num2);
 ~Student(void){};
public:
 T1 m_TNum1;
 T2 m_TNum2;
};
// 类外进行声明
template<class T1, class T2>
void Student<T1, T2>::add(T1 t_num1, T2 t_num2)
{
 T1 temp = 0;
 temp = t_num1 + t_num2;
 cout << temp <<endl;
}


 

以下为转载:

转自:http://blog.csdn.net/justin12zhu/article/details/3893422  感谢作者

1.类模板与模板类的概念

什么是类模板 一个类模板(也称为类属类或类生成类)允许用户为类定义一种模式,使得类中的某些数据成员、默写成员函数的参数、某些成员函数的返回值,能够取任意类型(包括系统预定义的和用户自定义的)。

  如果一个类中数据成员的数据类型不能确定,或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的、实际的类,而是代表着一类类。

类模板定义 定义一个类模板,一般有两方面的内容:

A.       首先要定义类,其格式为:

template <class T>

class foo

{

……

}

foo 为类名,在类定义体中,如采用通用数据类型的成员,函数参数的前面需加上T,其中通用类型T可以作为普通成员变量的类型,还可以作为const和static成员变量以及成员函数的参数和返回类型之用。例如:

template<class T>

class Test{

private:

    T n;

    const T i;

    static T cnt;

public:

    Test():i(0){}

    Test(T k);

    ~Test(){}

    void print();

    T operator+(T x);

};

B.       在类定义体外定义成员函数时,若此成员函数中有模板参数存在,则除了需要和一般类的体外定义成员函数一样的定义外,还需在函数体外进行模板声明

例如

template<class T>

void Test<T>::print(){

    std::cout<<"n="<<n<<std::endl;

    std::cout<<"i="<<i<<std::endl;

    std::cout<<"cnt="<<cnt<<std::endl;

 

}

如果函数是以通用类型为返回类型,则要在函数名前的类名后缀上“<T>”。例如:

template<class T>

Test<T>::Test(T k):i(k){n=k;cnt++;}

template<class T>

T Test<T>::operator+(T x){

               return n + x;

               }

C.       在类定义体外初始化const成员和static成员变量的做法和普通类体外初始化const成员和static成员变量的做法基本上是一样的,唯一的区别是需在对模板进行声明,例如

template<class T>

int Test<T>::cnt=0;

template<class T>

Test<T>::Test(T k):i(k){n=k;cnt++;}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值