关键字explicit理解

explicit 应用在构造函数中,其作用是禁止隐式转化 。何为隐式转化?

举例如下:

 class  TestA

{

    public:

          TestA(int n ) {……}

}

 

 class  TestB

{

    public:

          explicit  TestB(int n ) {……}

}

 

void main()

{

    TestA ta = 10;//正确

    TestB tb = 10;//错误,进行了隐式转换

    TestB tb(10);//正确

}

你可能感兴趣的:(Class)