c++中的explicit关键字

来源:互联网 发布:远东水利预算软件 编辑:程序博客网 时间:2024/06/02 03:35


explicit关键字很少使用,但在STL源文件中使用较多。


explicit的作用:主要为了限制默认构造函数的调用,引发未知的错误。

示例:

class Testclass{public:Testclass();explicit Testclass(int first, int second = 2);~Testclass();private:};Testclass::Testclass(){}Testclass::~Testclass(){}Testclass::Testclass(int first, int second){}int _tmain(int argc, _TCHAR* argv[]){Testclass testone;Testclass testtwo(1, 2);Testclass testthree = 1;//error:添加了explicit不能使用默认构造函数getchar();return 0;}


0 0