explicit表示禁止自动类型转换,作用于单参数构造函数。又是一个比较冷门的关键字:
#include <iostream> using namespace std; class Foo { public: Foo() { cout << "Foo constructed" << endl; } Foo(int n) { cout << "Foo constructed with " << n << endl; } }; class Child:public Foo { public: explicit Child(int n) { Foo::Foo(n); } }; ///////////////////////////SubMain////////////////////////////////// int main(int argc, char *argv[]) { Foo f(0); f = 1; Child c(2); // won't compile // c = 3; system("pause"); return 0; } ///////////////////////////End Sub//////////////////////////////////