C++ 修饰符
class MyClass
{
public:
MyClass(std::string name) { name_ = name;}
private:
std::string name_;
};
class MyClass1
{
public:
explicit MyClass1(std::string name) { name_ = name;}
private:
std::string name_;
};
void Fun(MyClass c)
{
//...
}
void Fun1(MyClass c)
{
//...
}
int main(int argc, char *argv[])
{
std::string name = "ZhangSan";
Fun(name); //正确
Fun1(name); //编译错误
Fun1(MyClass1(name)); //正确
MyClass1 obj0("ZhangSan"); //直接初始化,正确
MyClass1 obj1{"ZhangSan"}; //列表初始化,正确
MyClass1 obj2 = "ZhangSan"; //复制初始化,编译错误
MyClass obj3 = "ZhangSan"; //正确
return 0;
}