#include <iostream>
using namespace std;
class Test1{
private:
int n;
public:
Test1(int x){
n = x;
}
};
class Test2{
private:
int n;
public:
explicit Test2(int x){//普通构造函数能够被隐式调用。而explicit构造函数只能被显示调用。
n = x;
}
};
int main()
{
Test1 A=12;
cout << 11;
//Test2 B = 23;编译错误因为该构造函数使用了explicit.不能隐式调用其构造函数
Test2 B (23);
return 0;
}
C++中的explicit
最新推荐文章于 2023-04-05 11:53:39 发布