c++模版笔记
2013-12-06 14:36
113 查看
一、一般模板函数形式
上述代码在编译阶段,编译器其实是自动生成两个函数。模板原理由此可观一二
二、模板类
#include <iostream> using namespace std; //templae <typename type1,typename type2> //若有多个参参数可如此定义即可 template <typename type>//定义不定类型 const type& myMax(const type& valueOne,const type& valueTow) { if(valueOne >= valueTow) return valueOne; else return valueTow; } int main() { int intOne = myMax(45,32); double doubleOne = myMax(453.3,8923.9); cout<<"intOne = "<<intOne<<endl; cout<<"doubleTow = "<<doubleOne<<endl; int s87;cin>>s87; return 0; }
上述代码在编译阶段,编译器其实是自动生成两个函数。模板原理由此可观一二
二、模板类
#include <iostream> using namespace std; template <typename type>//定义不定类型 class classOne { public: static int staticValue; }; template <typename type> int classOne<type>::staticValue;//static value init int main() { classOne<int> intCase1,intCase2; intCase1.staticValue = 100; classOne<double> doubleCase1,doubleCase2; doubleCase1.staticValue = 200.999; cout<<"int class:staticValue="<<intCase2.staticValue<<endl; cout<<"double clas:staticValue="<<doubleCase2.staticValue<<endl; int s87;cin>>s87; return 0; }由上可知,编译器是根据不同 的static创建了两个没有关联的类,一个是int型的一个是double型的,他们是连个不同类。
相关文章推荐
- c++ 笔记 之 模版1
- C++模版学习笔记 非类型形参
- C++学习笔记36 模版的显式具体化(template specialization)和显式实例化(template instantiation)
- C++学习笔记35 方法模版
- 初探C++函数模版学习笔记
- C++学习笔记34 模版的原理
- C++学习笔记34 模版的原理
- C++容器学习笔记1——模版、迭代器
- C++模版的详细介绍(学习笔记)
- C++学习笔记:函数模版
- C++笔记_04C++模版1
- c++学习笔记,windows窗口模版
- C++学习笔记4--表达式
- 【C++学习笔记】使用new创建动态结构
- C/C++笔记
- C++库研究笔记——Linux 下高精度计时器
- [C++]C++学习笔记(五)
- C++ primer 学习笔记——向 量
- 【从C到C++学习笔记】内联成员函数/成员函数重载及缺省参数
- C++学习笔记(1月7日)