您的位置:首页 > 编程语言 > C语言/C++

explicit 关键字声明 不能隐式调用构造函数

2014-05-04 15:50 190 查看
class Test1
{
public:
Test1(int n) { num = n; } //普通构造函数
private:
int num;
};
class Test2
{
public:
explicit Test2(int n) { num = n; } //explicit(显式)构造函数
private:
int num;
};
int main()
{
Test1 t1 = 12; //隐式调用其构造函数, 成功
Test2 t2 = 12; //编译错误,不能隐式调用其构造函数
Test2 t3(12); //显式调用成功
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++