//可以运行
#include
using namespace std;
class ABC
{
private:
int x;
public:
explicit ABC(int
xx)
{
x = xx;
}
void
show()
{
cout<<x<<endl;
}
};
int main()
{
ABC abc(100);
abc.show();
return
0;
}
//可以运行
#include
using namespace std;
class ABC
{
private:
int x;
public:
ABC(int xx)
{
x =
xx;
}
void
show()
{
cout<<x<<endl;
}
};
int main()
{
ABC abc = 100;
abc.show();
return
0;
}
//无法运行通过
#include
using namespace std;
class ABC
{
private:
int x;
public:
explicit ABC(int
xx)
{
x =
xx;
}
void
show()
{
cout<<x<<endl;
}
};
int main()
{
ABC abc = 100;
abc.show();
return
0;
}
#include
using namespace std;
class ABC
{
private:
public:
};
int main()
{
}
//可以运行
#include
using namespace std;
class ABC
{
private:
public:
};
int main()
{
}
//无法运行通过
#include
using namespace std;
class ABC
{
private:
public:
};
int main()
{
}
本文展示了如何使用 C++ 中的显式构造函数来防止从单一参数的构造函数进行隐式转换。通过三个不同的示例对比,解释了显式关键字的作用及其对代码行为的影响。
613

被折叠的 条评论
为什么被折叠?



