/*******************************************************
*author:彭晓林
*copyright: 版权所有,翻版不究
*function: 防止s构造函数隐式转换示例
******************************************************/
#include <iostream>
#include <string>
using namespace std;
class DEMO
{
public:
explicit DEMO(int a)
{
x = a;
cout<<"调用构造函数"<<endl;
}
~DEMO()
{
cout<<"调用析构函数"<<endl;
}
private:
int x;
};
int main()
{
DEMO TestA(2);
TestA = DEMO(2);
while(1);
}
本文通过一个C++示例介绍了如何使用explicit关键字来阻止构造函数的隐式转换,这对于理解C++类的设计和使用至关重要。
260

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



