首页 > 代码库 > explict关键字
explict关键字
【本文链接】
http://www.cnblogs.com/hellogiser/p/explict.html
【分析】
explicit 只对构造函数起作用,用来抑制隐式转换。
Suppose you have a class String
C++ Code
1 2 3 4 5 6 | class String { public: String(int n); // allocate n bytes to the String object String(const char *p); // initializes object with char *p } |
Now if you try
C++ Code
1 | String mystring = ‘x‘; // String mystring = String(‘x‘); |
the char ‘x‘ will be converted to int and will call String(int) constructor. But this is not what the user might have intended. So to prevent such conditions, we can define the class‘s constructor as explicit.
C++ Code
1 2 3 4 5 6 | class String { public: explicit String (int n); //allocate n bytes String(const char *p); // initialize sobject with string p } |
【参考】
http://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-in-c-mean
http://www.cnblogs.com/cutepig/archive/2009/01/14/1375917.html
explict关键字
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。