C++模板常用使用方法介绍
转载:http://developer.51cto.com/art/201002/182202.htm
C++编程语言中的模板应用在一定程度上大大提高了程序开发的效率。我们在这篇文章中为大家详细讲解一下有关C++模板的基本概念,希望初学者们可以通过本文介绍的内容充分掌握这方面的知识。
前段时间重新学习C++,主要看C++编程思想和C++设计新思维。对模版的使用有了更进一层的了解,特总结如下:
下面列出了C++模板的常用情况:
1. C++模板类静态成员
template < typename T> struct testClass
{
static int _data;
};
template< > int testClass< char>::_data = ;
template< > int testClass< long>::_data = ;
int main( void ) {
cout < < boolalpha < < (==testClass< char>::_data) < < endl;
cout < < boolalpha < < (==testClass< long>::_data) < < endl;
}
2. C++模板类偏特化
template < class I, class O> struct testClass
{
testClass() { cout < < "I, O" < < endl; }
};
template < class T> struct testClass< T*, T*>
{
testClass() { cout < < "T*, T*" < < endl; }
};
template < class T> struct testClass< const T*, T*>
{
testClass() { cout < < "const T*, T*" < < endl; }
};
int main( void )
{
testClass< int, char> obj1;
testClass< int*, int*> obj2;
testClass< const int*, int*> obj3;
}
3.类模版+函数模版
template < class T> struct testClass
{
void swap( testClass< T>& ) { cout < < "swap()" < < endl; }
};
template < class T> inline void swap( testClass< T>& x,
testClass< T>& y )
{
x.swap( y );
}
int main( void )
{
testClass< int> obj1;
testClass< int> obj2;
swap( obj1, obj2 );
}
4. 类成员函数模板
struct testClass
{
template < class T> void mfun( const T& t )
{
cout < < t < < endl;
}
template < class T> operator T()
{
return T();
}
};
int main( void )
{
testClass obj;
obj.mfun( );
int i = obj;
cout < < i < < endl;
}
5. 缺省C++模板参数推导
template < class T> struct test
{
T a;
};
template < class I, class O=test< I> > struct testClass
{
I b;
O c;
};
void main()
{
}
6. 非类型C++模板参数
template < class T, int n> struct testClass {
T _t;
testClass() : _t(n) {
}
};
int main( void ) {
testClass< int,> obj1;
testClass< int,> obj2;
}
7. 空模板参数
template < class T> struct testClass;
template < class T> bool operator==( const testClass< T>&,
const testClass< T>& )
{
return false;
};
template < class T> struct testClass
{
friend bool operator== < >
( const testClass&, const testClass& );
};
void main()
{
}
8. template template 类
struct Widget1
{
template< typename T>
T foo(){}
};
template< template< class T>class X>
struct Widget2
{
};
void main()
{
cout< < < < '\n';
}
以上就是对C++模板的一些常用方法的介绍。
C++模板常用使用方法介绍的更多相关文章
- WebBrowser常用属性方法介绍
WebBrowser 常用属性方法 ■■方法 ============================== ▲GoBack 相当于IE的"后退"按钮,使你在当前历史列表中后 ...
- thinkphp模板常用的方法
thinkphp模板我是看了3.2的文档,对里面的东西过了一遍,然后在写到需要用到模板的东西的时候就有印象,有的能直接回顾,但是有的就可能只知道有这个东西,但是不知道怎么用,所以就重新查手册,这个的话 ...
- Node.js GET/POST对应的url/query-string常用的方法介绍
<一>,在学node.js--GET/POST请求时,先看模块url和query-string的用法 1. 模块url用法,一般用于解析get请求. parse: [Function: u ...
- Python中的常用魔术方法介绍
1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...
- winFrom 常用控件属性及方法介绍
目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文本框)控件 4.RichTextBox控件 5.NumericUpDown控件 6.Button(按钮)控件 7.Gro ...
- C# 常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox ...
- 【转载】C#常用控件属性及方法介绍
C#常用控件属性及方法介绍 目录 1.窗体(Form) 2.Label (标签)控件 3.TextBox(文 ...
- SVG DOM常用属性和方法介绍(1)
12.2 SVG DOM常用属性和方法介绍 将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析 ...
- 浅析VS2010反汇编 VS 反汇编方法及常用汇编指令介绍 VS2015使用技巧 调试-反汇编 查看C语言代码对应的汇编代码
浅析VS2010反汇编 2015年07月25日 21:53:11 阅读数:4374 第一篇 1. 如何进行反汇编 在调试的环境下,我们可以很方便地通过反汇编窗口查看程序生成的反汇编信息.如下图所示. ...
随机推荐
- FileStream大文件复制
FileStream缓冲读取和写入可以提高性能.FileStream读取文件的时候,是先讲流放入内存,经Flash()方法后将内存中(缓冲中)的数据写入文件.如果文件非常大,势必消耗性能.特封装在Fi ...
- Struts2 数据校验流程
- js实现开灯关灯效果
<!DOCTYPE html> <html> <body> <script> function changeImage() { element=docu ...
- 克隆选择算法-python实现
CSAIndividual.py import numpy as np import ObjFunction class CSAIndividual: ''' individual of clone ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- 破解受保护的excel中的密码
今天朋友给我发了一张excel表,她说不能删除数据,让我帮忙弄弄,我当时就觉得这张表应该是受到保护了,就在视图选项中准备撤销保护,但是却需要密码,后来在网上查找了 下资料,发现有个方法可以将密码破解出 ...
- HBase概念学习(十)HBase与MongDB等NoSQL数据库对照
转载请注明出处: jiq•钦's technical Blog - 季义钦 一.开篇 淘宝之前使用的存储层架构一直是MySQL数据库,配合以MongDB,Tair等存储. MySQL因为开源,而且生态 ...
- AndroidManifest File Features
http://www.android-doc.com/guide/topics/manifest/manifest-intro.html The following sections describe ...
- Thinkphp中验证码的使用以及验证的实现
<input class="TxtValidateCodeCssClass" id="captcha" name="captcha" ...
- PHP函数篇详解十进制、二进制、八进制和十六进制转换函数说明
PHP函数篇详解十进制.二进制.八进制和十六进制转换函数说明 作者: 字体:[增加 减小] 类型:转载 中文字符编码研究系列第一期,PHP函数篇详解十进制.二进制.八进制和十六进制互相转换函数说明 ...