目录
explicit:该构造函数是显示的。
implicit:该构造函数是隐式的(默认)
说白了就在在构造函数只有一个时能不能使用=号直接赋值。
如果是显示的会被阻止,在编译时无法自动转为对象。
如果时隐私的会被放行。
看看一下代码:
#include <iostream>
using namespace std;
class Cat
{
public :
Cat(int num):n(num){} // 默认为 implicit
private:
int n;
};
class Mouse
{
public :
explicit Mouse(int num):n(num){}
private:
int n;
};
int main()
{
Cat t1 = 12; // 编译通过12会自动转为Cat对象赋值到构造函数
Mouse t2(13); // 编译通过,
Mouse t