C++关键字之explicit c++中一个关键字为explicit,这个关键字是用来表明系统不能默认调用类的转换构造函数,要进行类型转换构造函数的调用,需要显示调用。 Demo代码如下: /* FileName: explicitDemo.cpp Author: ACb0y Create Time: 2011年3月3日11:49:23 */ #include <iostream> using namespace std; //测试类 class Test { private: char * pStr; public: explicit Test(char *); Test(); void print(); }; Test::Test() { pStr = NULL; } Test::Test(char * str) { if (NULL != pStr) { free(pStr); } pStr = (char *)malloc(strlen(str) + 1); strcpy(pStr, str); } void Test::print() { printf("%s/n", pStr == NULL ? "NULL" : pStr); } int main() { Test t; t.print(); /* t = "hello world"; t.print(); */ t = (Test)"hello world"; t.print(); return 0; } 运行结果如下: