目录
- explicit关键字
- 总结
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;
}
|
编译失败!
总结
本片文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!
原文链接:https://blog.csdn.net/L1558198727/article/details/119974918








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。