作用:防止构造函数中的隐式类型转换。
#include <iostream>
#include <vector>
using namespace std;
class MyString {
public:
MyString(const char* str) {
}
explicit MyString(int a) {
mSize = a;
}
char* mStr;
int mSize;
};
void test01() {
MyString str = "abc";
MyString str2(10);
MyString str3 = 10; // 做什么用途?str2字符串为"10"? 字符串长度为10?存在二义性
// 相当于隐式类型转换 MyString str3 = MyString(10)
// 不想有二义性那就取消隐式类型转换,构造函数加上explicit关键词
}