explicit&用法&作用
2016-04-04 19:08
225 查看
/* explicit: 1,使用在一个参数的构造函数 2,使用之后该类的对象只能显示的调用构造函数 不能发生隐式的类型转换 */ // eg: #include <iostream> using namespace std; class test1 //未使用explicit { private: double a; int b; public: test1(int b_); test1(double a_, int b_ = 0); }; class test2 //使用explicit { private: double a; int b; public: explicit test2(int b_); explicit test2(double a_, int b_ = 0); }; test1::test1(int b_):b(b_) { cout << "警告!\a正在使用构造函数1\n"; } test1::test1(double a_, int b_):a(a_),b(b_) { cout << "警告!\a正在使用构造函数2\n"; } test2::test2(int b_):b(b_) { cout << "警告!\a正在使用构造函数3\n"; } test2::test2(double a_, int b_):a(a_),b(b_) { cout << "警告!\a正在使用构造函数4\n"; } void main() { //未使用explicit test1 one(2); one = 3; //本来不该使用构造函数的 但是确实是调用了 且不易被察觉(当然这里为了试验在构造函数里给出了提示 容易被发现) //使用explicit test2 two(3.6); // two = 3.4; //错误 1 error C2679: 二进制“=”: 没有找到接受“double”类型的右操作数的运算符(或没有可接受的转换) system("pause"); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android Native 绘图方法
- Python动态类型的学习---引用的理解
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 土人系列AS入门教程 -- 对象篇
- 文件遍历排序函数
- C#托管堆对象实例包含内容分析
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- C#中struct和class的区别详解
- C#实现获取不同对象中名称相同属性的方法
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解