c++学习点滴4
2013-01-21 10:41
225 查看
一、函数模板
二、explicit关键字
主要是用来禁止隐式转换。所有单参数构造函数、大多数情况下的拷贝构造函数都应该被声明为explicit
参考/article/7142207.html
1. 一些注意事项 typedef double Type; template <class Type> // 1. 这里的Type会覆盖全局域中的Type名字。即这里的Type不是double const Type& min(const Type& v1, const Type& v2) { //typedef char Type; // 2. 不能再声明和模板参数类型同名的类型 Type tmp = v1 < v2 ? v1 : v2; return tmp; } // 函数声明和定义中的参数类型名不必相同 template <typename T> void fun (const T& t1); // 声明 template <typename U> void fun(const U& t1); // 定义 2.函数模板的实例化 template<class T> void fun(T t){ cout << "void fun(T t) " << endl; } void test() { // 函数模板在被调用或取地址的时候被实例化 fun(3); // 被实例化 void (*pf)(int) = &fun; // 被实例化 pf(10); } 3. 对于如下函数模板定义 template<class T, int size> void fun(T (&rarr)[size]){ } void test(int arr[3]) { //fun(arr); // error. arr是int*类型而非int[]类型 int arr2[] = {1,2,3}; fun(arr2); }
二、explicit关键字
主要是用来禁止隐式转换。所有单参数构造函数、大多数情况下的拷贝构造函数都应该被声明为explicit
参考/article/7142207.html
相关文章推荐
- C++学习点滴1
- 【C++学习点滴】4月汇总:volatile,数组维度,const修饰符
- C++学习点滴 函数的工作原理 按值传递按址传递
- 【C++学习点滴】4月汇总:volatile,数组维度,const修饰符
- 【C++学习点滴】关于参数传递的问题
- c++学习点滴
- c++学习点滴3
- 【C++学习点滴】关于参数传递的问题
- C++学习点滴
- c++学习点滴6
- C++ 学习拾遗 —— 点滴记录C++学习过程中遇到的问题以及整理
- c++学习点滴5
- C++学习点滴--关于private作用域问题
- C++ 学习拾遗 —— 点滴记录C++学习过程中遇到的问题以及整理
- c++学习点滴
- C++ 学习点滴 声明与定义的区别 内联函数
- C++ prime plus学习点滴记录
- c++学习点滴2
- opencv学习c++点滴
- 记录自己学习C++的点滴,(高手请指点) 偶能承受打击