C++ explicit关键字

通过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 }

 

posted @ 2016-10-21 14:31  IT男汉  阅读(179)  评论(0编辑  收藏  举报