【c++】explicit 隐式类类型转换
2013-12-18 08:37
537 查看
上代码
究其因
same_s应该接收一个类A的对象参数,但是这里直接传了一个字符串对象。过程这样:编译器接收一个string的对象,利用构造函数 A(const string &book = "ab") 生成临时对象(隐性转换),然后传递给same_s,临时对象失效.
注意: explicit 只能用于类内部的构造函数的声明上。
explicit和构造函数一起使用.
explicit constructor指明构造函数只能显式使用,目的是为了防止不必要的隐式转化.
如何防止隐性转换
构造函数加关键字explicit
结果出错
![](http://images.cnitblog.com/blog/408927/201312/18083328-80a9cfd9ce874edeadede7ece5e3d69a.png)
解决之道
字符串强制传递给构造函数
同理
对于 A a = string("hello")来说,如果参数为string的构造函数为explicit,则这样初始化是错误的;如果不是explicit,则这样初始化是正确的(先利用构造函数生成临时对象,再把临时对象通过赋值操作符复制到新创建的对象上)。
对比实验
对
![](http://images.cnitblog.com/blog/408927/201312/19215057-cf8c2ab484ec48b7a462330edab3d310.png)
错
#include <iostream> #include <sstream> using namespace std; class A { public: A(const string &book = "ab") : s(book) {} int same_s(const A &a) const { return s == a.s; } private: string s; }; int main(int argc ,char **argv) { A c("aaa"); string m = "aaa"; cout << c.same_s(m) << endl; }
究其因
same_s应该接收一个类A的对象参数,但是这里直接传了一个字符串对象。过程这样:编译器接收一个string的对象,利用构造函数 A(const string &book = "ab") 生成临时对象(隐性转换),然后传递给same_s,临时对象失效.
注意: explicit 只能用于类内部的构造函数的声明上。
explicit和构造函数一起使用.
explicit constructor指明构造函数只能显式使用,目的是为了防止不必要的隐式转化.
如何防止隐性转换
构造函数加关键字explicit
#include <iostream> #include <sstream> using namespace std; class A { public: explicit A(const string &book = "ab") : s(book) {} int same_s(const A &a) const { return s == a.s; } private: string s; }; int main(int argc ,char **argv) { A c("aaa"); string m = "aaa"; cout << c.same_s(m) << endl; }
结果出错
![](http://images.cnitblog.com/blog/408927/201312/18083328-80a9cfd9ce874edeadede7ece5e3d69a.png)
解决之道
字符串强制传递给构造函数
#include <iostream> #include <sstream> using namespace std; class A { public: explicit A(const string &book = "ab") : s(book) {} int same_s(const A &a) const { return s == a.s; } private: string s; }; int main(int argc ,char **argv) { A c("aaa"); string m = "aaa"; cout << c.same_s(A(m)) << endl; }
同理
对于 A a = string("hello")来说,如果参数为string的构造函数为explicit,则这样初始化是错误的;如果不是explicit,则这样初始化是正确的(先利用构造函数生成临时对象,再把临时对象通过赋值操作符复制到新创建的对象上)。
对比实验
对
#include <iostream> #include <sstream> using namespace std; class A { public: A(const string a) : s(a) {} private: string s; }; int main(int argc ,char **argv) { A a = string("hello"); }
![](http://images.cnitblog.com/blog/408927/201312/19215057-cf8c2ab484ec48b7a462330edab3d310.png)
错
#include <iostream> #include <sstream> using namespace std; class A { public: explicit A(const string a) : s(a) {} private: string s; }; int main(int argc ,char **argv) { A a = string("hello"); }
相关文章推荐
- 【C++】隐式类型转换和 explicit关键字
- c++隐式类型转换和explicit
- C++ 隐式类型转换与关键字explicit
- c++中的explicit关键字及隐式类型转换
- C++隐式的类类型转换,explicit关键字介绍。
- C++ 隐式类型转换与关键字explicit
- c++中的explicit关键字及隐式类型转换
- 从Qt谈到C++(一):关键字explicit与隐式类型转换
- C++的类类型隐式转换与explicit关键字
- C++12.4.4 隐式类类型转换----explicit
- explicit:C++规定,当定义了只有一个参数的构造函数时,同时也定义了一种隐式的类型转换
- c++的转换构造函数和杜绝隐式类型转换的explicit关键字
- c++中的explicit关键字及隐式类型转换
- c++中的explicit关键字及隐式类型转换
- c++中的explicit关键字及隐式类型转换
- More Effective C++ 阅读笔记(四)-- 避免使用隐式类型转换
- 隐式类型转换c/c++
- 关于C++中的类型转换(隐式转换)
- C++类型的隐式转换和显式转换
- [C++再学习系列] 隐式类型转换与转换操作符