下载此文档

explicit关键字的作用.doc


文档分类:IT计算机 | 页数:约5页 举报非法文档有奖
1/5
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/5 下载此文档
文档列表 文档介绍
谈谈explicit关键字
2004-08-19 20:35 16677人阅读 评论(7) 收藏 举报
今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案::
// Copy From MSDN
This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.
The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g.
//
// compile with: /EHsc
#include
class C
{
public:
int i;
explicit C(const C&) // an explicit copy constructor
{
printf("/nin the copy constructor");
}
explicit C(int i ) // an explicit constructor
{
printf("/nin the constructor");
}
C()
{
i = 0;
}
};
class C2
{
public:
int i;
explicit C2(int i ) // an explicit constructor
{
}
};
C f(C c)
{ // C2558
= 2;
return c; // first call to copy constructor
}
void f2(C2)
{
}
void g(int i)
{
f2(i); // C2558
// try the following line instead
// f2(C2(i));
}
int main()
{
C c, d;
d = f(c); // c is copied
}
Note   explicit on a constructor with multiple arguments has no effect, since such const

explicit关键字的作用 来自淘豆网www.taodocs.com转载请标明出处.

非法内容举报中心
文档信息
  • 页数5
  • 收藏数0 收藏
  • 顶次数0
  • 上传人qiang19840906
  • 文件大小24 KB
  • 时间2021-03-01