防止隐式类型转换,explicit关键字

防止隐式类型转换

class MyString
{
public:
	MyString(const char * str) {
	}
	//explicit关键字,防止隐式类型转换
	explicit MyString(int a) {
		mSize = a;
	}
	
	char* mStr;
	int mSize;
};
void test01()
{
	MyString str = "abc";
	//MyString str2 = 10;
	//隐式类型转换 Mystring str3 = Mystring(10);
}

posted on 2021-04-22 17:47  lodger47  阅读(34)  评论(0)    收藏  举报

导航