函数模板与类模板有什么区别?答:函数模板的实例化是由编译程序在处理函数调用时自动完成的,而类模板的实例化
必须由程序员在程序中显式地指定。
即函数模板允许隐式调用和显式调用而类模板只能显示调用
这期间有涉及到函数模板与模板函数,类模板与模板类的概念 (类似于类与类对象的区别)
请看下面例子
注意:模板类的函数声明和实现必须都在头文件中完成,不能像普通类那样声明在.h文件中实现在.cpp文件中,原因可以看链接http://hi.baidu.com/cn_rigel/blog/item/6cf6fc083723e2286a60fb53.html
#
include "stdafx.h"
// 函数模板的实例化在程序调用时自动完成
# include
< iostream
>
using namespace
std ;
//使用模板创建一个返回最大值的函数
//这是一个函数模板
template <
class Type>
Type MaxValue( Type a, Type b)
{
if ( a
> b)
{
return a;
}
else
return b;
}
//创建一个堆栈模板类
//这是一个类模板
template <
class T>
class Stack
{
public :
Stack (
) { m_nPos
= 0; }
~ Stack
( )
{ }
void Push( T value)
;
T Pop( )
;
bool IsEmpty(
)
{
return m_nPos
= = 0;
}
bool HasElement(
)
{
return ! IsEmpty(
) ;
}
bool IsFull(
)
{
return m_nPos
= = STATCK_SIZE;
}
private :
int m_nPos;
//使用常量表示堆栈的大小
const static
int STATCK_SIZE = 100;
T m_Data[ STATCK_SIZE]
;
} ;
//模板类的成员函数实现
template <
class T>
void Stack
< T> :
: Push( T value)
{
//使用后置递增操作符
m_Data[ m_nPos+
+ ]
= value;
}
template <
class T>
T Stack < T>
: : Pop(
)
{
//使用前置递减操作符
return m_Data[
- - m_nPos]
;
}
void TestMaxValue(
)
{
//隐式调用
cout
<
<
MaxValue(
100 , 204) <
< endl
; //MaxValue(
100 , 204) 这是一个模板函数
cout <
< MaxValue( 2. 5002, 30. 003)
< <
endl ; //MaxValue( 2. 5002, 30. 003)这也是一个模板函数
//当然由程序员自己指定也可以
//显示调用
cout <
< MaxValue<
int > ( 10, 20)
< <
endl ;
cout <
< MaxValue<
double > ( 2. 5002, 30. 003)
< <
endl ;
}
void TestStack(
)
{
//测试模板类(整数)
Stack <
int > intStack; //类模板的实例化由程序员显示的指定
intStack. Push( 10)
;
intStack. Push( 20)
;
intStack. Push( 30)
;
while ( intStack. HasElement(
) )
{
cout <
< intStack. Pop(
) <
< endl ;
}
//测试模板类(浮点)
Stack <
float > floatStack; //类模板的实例化由程序员显示的指定
floatStack. Push( 1. 001)
;
floatStack. Push( 2. 002)
;
floatStack. Push( 3. 003)
;
while ( floatStack. HasElement(
) )
{
cout <
< floatStack. Pop(
) <
< endl ;
}
//测试动态创建对象
//Stack创建的指针必须指明类型
Stack <
int >
* pInt = new
Stack <
int > (
) ; 类模板的实例化由程序员显示的指定
pInt- > Push( 10);
pInt- > Push( 20)
;
pInt- > Push( 30)
;
while ( pInt-
> HasElement(
) )
{
cout <
< pInt-
> Pop( )
< <
endl ;
}
if ( pInt
! =
NULL )
{
delete pInt;
pInt = NULL
;
}
}