explicit

explicit is a key word to prevent the constructor to do the implicit type conversion

 

class String1 {
public:
    String1(int n); // assign n bytes
}

class String2 {
public:
    explicit String2(int n); // assign n bytes
}


String1 str1 = 'x';  // OK: 'x' will be converted to int and calls the String1(int) constructor
String2 str2 = 'x'; // ERROR: constructor can not do the conversion

reference: http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean

posted @ 2013-10-18 19:31  LevyFan  阅读(182)  评论(0编辑  收藏  举报