加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C 11委托执行程序比调用init函数的C 03 ctors更糟糕?

发布时间:2020-12-16 05:44:27 所属栏目:百科 来源:网络整理
导读:[这个问题已被高度编辑;恕我直言,我已将编辑移到下面的答案中] 从07年7月11日 This [ new delegating constructors feature ] comes with a caveat: C++03 considers an object to be constructed when its constructor finishes executing,but C++11 consid
[这个问题已被高度编辑;恕我直言,我已将编辑移到下面的答案中]

从07年7月11日

This [new delegating constructors feature] comes with a caveat: C++03 considers an object to be constructed when its constructor finishes executing,but C++11 considers an object constructed once any constructor finishes execution. Since multiple constructors will be allowed to execute,this will mean that each delegating constructor will be executing on a fully constructed object of its own type. Derived class constructors will execute after all delegation in their base classes is complete.”

这是否意味着委托链为ctor代理链中的每个链接构建一个唯一的临时对象?只是为了避免简单的init函数定义,这种开销不值得额外的开销.

免责声明:我问这个问题,因为我是一名学生,但迄今为止的答案都是不正确的,并且表明缺乏对所提及的研究和/或理解.我对此感到沮丧,结果我的编辑和评论匆匆而过,主要是通过智能手机.请原谅我希望我在下面的答案中尽量减少了这一点,我已经了解到,我的意见中需要谨慎,完整和清楚.

解决方法

不,他们是等同的.委托构造函数的行为就像一个普通的成员函数,它作用于由前一个构造函数构造的Object.

我在proposal for adding delegating constructors中找不到明确支持这一点的信息,但在一般情况下创建副本是不可能的.某些类可能没有复制构造函数.

在第4.3节 – 对§15的修改中,建议对标准状态的更改:

if the nonヾelegating constructor for an object has completed execution and a delegating constructor for that object exits with an exception,the object’s destructor will be invoked.

这意味着委托构造函数在完全构造的对象上工作(取决于如何定义它),并允许实现使委托函数像成员函数一样工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读