explicit关键字
用来放置类进行隐式转换
例如一个类有一个形参是int的构造函数
如下,在Pos的vector push的时候 ,直接使用一个int 就可以隐式转换为Pos
如果不想被隐式转换 就加上explicit关键字
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <iostream> #include <tuple> #include <queue> #include <stack> #include <list> using namespace std; #define debug(x) cout<<#x<<": "<<(x)<<endl; class Pos { public: Pos() { } Pos(int x) { } }; int main(int argc, const char* argv[]) { vector<Pos> arr; //arr.reserve(1e5); for (int i = 0; i < 1e5; ++i) { arr.push_back(1); } return 0; }
编译成功!
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <iostream> #include <tuple> #include <queue> #include <stack> #include <list> using namespace std; #define debug(x) cout<<#x<<": "<<(x)<<endl; class Pos { public: explicit Pos() { } explicit Pos(int x) { } }; int main(int argc, const char* argv[]) { vector<Pos> arr; //arr.reserve(1e5); for (int i = 0; i < 1e5; ++i) { arr.push_back(1); } return 0; }
编译失败!
总结
本片文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注靠谱客的更多内容!
最后
以上就是可爱蓝天最近收集整理的关于C++ Effective详解的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复