通过explicit关键字,可以阻止“单参构造函数”自动型转换。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class Student { 5 private : 6 string name; 7 public: 8 explicit Student(char* name){ 9 this->name = name; 10 } 11 } 12 int main(){ 13 Student s ="liuteng";//不正确 14 }