explicit作用:
在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换。

#include<iostream>
using namespace std;

class Test
{
public:
explicit Test(int a)//构造函数加explicit修饰,不允许发生隐式类型装换
{
number
=a;
}
private:
int number;
};
int main()
{
Test test(
10);//这样总是对的
Test test2=10;//当构造函数加入explicit修饰时,程序将会编译不通过
return 0;
}

相关文章:

  • 2022-12-23
  • 2021-12-15
  • 2021-07-08
  • 2022-01-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2021-08-02
  • 2021-10-05
相关资源
相似解决方案