- 当构造函数只有一个构造函数时,可以直接将参数赋值给类对象
class Entity {
public:
String m_Name;
int m_Age;
public:
explicit Entity(const String& name) :m_Name(name) ,m_Age(0){}
Entity(int age) :m_Name("Unkown"),m_Age(age) {}
};
Entity e1 = 22;
Entity e2 = std::string("wkx");
- 使用explicit禁用隐式构造函数
class Entity {
public:
String m_Name;
int m_Age;
public:
explicit Entity(const String& name) :m_Name(name) ,m_Age(0){}
Entity(int age) :m_Name("Unkown"),m_Age(age) {}
};
//隐式构造函数
Entity e1 = 22;
//Entity e2 = std::string("wkx");//使用explicit禁用了隐式构造函数
标签:Name,Age,explicit,Entity,隐式,构造函数 From: https://www.cnblogs.com/wangkaixin-yy/p/17649625.html