概述
explicit只能修饰构造函数,构造函数默认implicit,implicit不是keyword
- implicit构造函数:可用于直接初始化,也可用于复制初始化
- explicit构造函数:可用于直接初始化,不可用于复制初始化
implicit
class CAnimal
{
public:
CAnimal(int group) : mGroup(group)
{
cout << "CAnimal(" << group << ")" << endl;
}
CAnimal(const CAnimal &other) : mGroup(other.mGroup)
{
cout << "CAnimal(const CAnimal &other) " << mGroup << endl;
}
CAnimal& operator=(const CAnimal &other)
{
mGroup = other.mGroup;
cout << "CAnimal operator= " << mGroup << endl;
return *this;
}
private:
int mGroup;
};
void implicit_construct()
{
{
cout << "-----init-----" << endl;