explicit关键字的作用_第1页
explicit关键字的作用_第2页
explicit关键字的作用_第3页
explicit关键字的作用_第4页
explicit关键字的作用_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

谈谈explicit关键字 2004-08-19 20:35 16677人阅读 评论(7) 收藏 举报 今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案:原来explicit是为了防止隐式使用拷贝构造函数的.以下附上从新版MSDN中找到的例子和网上那篇文章:/ Copy From MSDNThis 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./ spec1_explicit.cpp/ 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 C2public: int i; explicit C2(int i ) / an explicit constructor ;C f(C c) / C2558 c.i = 2; return c; / first call to copy constructorvoid 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 copiedNoteexplicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value./ Copy From Internet ArticlePointer不看书不知道自己的C+有多差T_T,看到 explicit 时遇到一个问题。请看下面一段程序:class Apublic:A(int i) : m_i(i)int m_i;int main()A a = 0;a = 10; / 这里是什么操作?这个操作产生了一个临时对象。我怀疑是默认赋值运算符 “A &operator = (int i)”,于是重载了一下该运算符,结果确实运行到重载的运算符函数里了,那么临时对象又是如何产生的呢?难道默认的赋值运算符是这样操作的?A &operator = (int i)A a(i);return a;这让我想起了类似的函数操作:void fn(A a)/ .这里可以直接写fn(10);也是产生了一个临时对象。难道真的是这样吗?忘解惑。alexeyomux老兄你用的是哪个编译器?在我印象之中,好像标准C+并不会给出operator =啊。等我去试一试。gongminmin当然会有默认的operator=了按照c+标准,编译器会生成五个默认成员函数:默认构造函数拷贝构造函数析构函数operator=operator&千里马肝class Apublic:A(int i) : m_i(i)int m_i;分别说说吧:1. A a = 0;首先, compiler认为这样写是不符合规矩的, 因为A = A才是正常行为。但是她并不放弃, 通过搜索, 发现A可以根据一个int构造, 同时这个A(int i)没有用explicit修饰过。那么A a = 0; 这样的一句话随即转变成:A tmp(0);A a = tmp;需要说明的是, A a = tmp是调用的copy ctor, 虽然class A中并没有, 但是通常不写copy ctor的话,compiler都会生成一个memberwise assignment操作性质的ctor, 底层实现通常会以memcpy进行。2. a = 10;首先, 这样同ctor的情况一样, compiler无法直接进行操作。类推, 等同于代码:A tmp(10);a = tmp;需要注意的是, a = tmp是调用的assignment操作, 同ctor一样,我们自己不写, 编译器同样进行memberwise assignment操作。3. fn(A a)同样, fn(10)也是不对的, 但是按照惯例, 呵呵, 会有:A tmp(10);fn(tmp); 另外, 为你解惑:copy ctor的写法只能是T:T(const T &);而assignment的写法可以多变, 即任意. 以T为例, 可以有T &operator = (int n);也可有T &operator = (const char *);当然, 你要确认如此的定义是对T而言有意义.然后, 上述a = tmp, 即调用的默认的、标准的、自动生成的T &operator = (const T &). 开销是会有一个临时的A tmp生成, 然后memcpy.但如果你自已写了T &operator = (int n), 那么a = 10即意味着a.m_i = 10. 当然, 以开销而言要视你的T &operator = (int n)是否为inline了.对于explicit, 当修饰explicit A(int i) : m_i(i), 那么即告诉compiler不要在私底下做那么多的转换动作.而且自动生成如A tmp(0)这样的东西是

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论