函数模板与类模板
2016-06-20 19:44
281 查看
函数模板与类模板有什么区别?答:函数模板的实例化是由编译程序在处理函数调用时自动完成的,而类模板的实例化
必 须由程序员在程序中显式地指定。
即函数模板允许隐式调用和显式调用而类模板只能显示调用
这期间有涉及到函数模板与模板函数,类模板与模板类的概念 (类似于类与类对象的区 别)
请看下面例子
注意:模板类的函数声明和实现必须都在头文件中完成,不能像普通类那样声明在.h文件中实现在.cpp文件中,原因可以看链接http://hi.baidu.com/cn_rigel/blog/item/6cf6fc083723e2286a60fb53.html
204) < < endl ; //MaxValue
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 ;
}
}
必 须由程序员在程序中显式地指定。
即函数模板允许隐式调用和显式调用而类模板只能显示调用
这期间有涉及到函数模板与模板函数,类模板与模板类的概念 (类似于类与类对象的区 别)
请看下面例子
注意:模板类的函数声明和实现必须都在头文件中完成,不能像普通类那样声明在.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 ;
}
}
相关文章推荐
- Android开发笔记(十一)Activity周期
- zookeeper启动。
- jQuery事件
- Develog--Training系列(Building Your FirstApp 第一节)
- 会议纪要的书写格式
- 影响 Linux 系统安全基石的 glibc 严重漏洞
- 成绩累加排名,poj(2153)
- 用RotateDrawable实现网易云音乐唱片机效果
- 动态规划--素数伴侣
- 8-130 万能计算器(支持各类数据的加法函数)
- 小记
- [HIHO1322]树结构判定(并查集)
- 【Struts 1】Struts1的基本原理和简介
- 【Struts 1】Struts1的基本原理和简介
- 安装swftool过程报错记录
- 第十六周上机实践项目2(1):阅读程序
- Java线程池
- 转:数字签名是什么?(阮一峰)
- java 工厂模式
- 网盘多个账号混淆问题