explicit关键字——C++

来源:互联网 发布:centos 7 解压缩tar 编辑:程序博客网 时间:2024/06/06 00:46
//可以运行
#include
using namespace std;

class ABC
{
private:
    int x;
public:
    explicit ABC(intxx)
    {
      x = xx;
    }
    voidshow()
    {
      cout<<x<<endl;
    }
};

int main()
{
    ABC abc(100);
   abc.show();
    return0;
}

//可以运行
#include
using namespace std;

class ABC
{
private:
    int x;
public:
    ABC(int xx)
    {
       x =xx;
    }
    voidshow()
    {
      cout<<x<<endl;
    }
};

int main()
{
    ABC abc = 100;
   abc.show();
    return0;
}

//无法运行通过
#include
using namespace std;

class ABC
{
private:
    int x;
public:
    explicit ABC(intxx)
    {
       x =xx;
    }
    voidshow()
    {
      cout<<x<<endl;
    }
};

int main()
{
    ABC abc = 100;
   abc.show();
    return0;
}
0 0
原创粉丝点击