在c++中
explicit 关键字可以阻止构造函数的隐式调用;
隐式调用是指在构造函数只有一个参数BigInteger(int a);
,或者不止一个参数但是其他参数有默认值BigInteger(int a, int b = 0 ,int c = 9.....);
此时
BigInteger A = 9;//这时候会隐式地调用BigInteger(int a)的构造函数;
但是如果在构造函数前面加上explicit关键字时:explicit BigInteger(int a);
BigInteger A = 9;//错误,不能再隐式调用构造函数;
BigInteger A(9);//正确
此外强调一点,explicit 只在类中声明时要加,写具体写的时候不需要;
explicit BigInteger(const string& number);
BigInteger::BigInteger(const string& number);