子对象初始化的顺序是什么? [英] What is the order of subobject initialization?

查看:288
本文介绍了子对象初始化的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们有一个某些类型的类的对象o,其中包含另一种类型的成员子对象sosso.考虑以下示例:

Let we have an object o of class some type which contains member subobjects so and sso of another class types. Consider the following example:

#include <iostream>
using namespace std;
    struct SO{ SO(){ cout << "SO()" << endl; } };
    struct SSO{ SSO(){ cout << "SSO()" << endl; } };

        struct O
        {
            O(){ cout << "O()" << endl; }
            SO so;
            SSO sso;
        };
    int main()
    {
        O o = *(new O);
    }

输出:

SO()

SSO()

O()

演示

第5.3.4节所述:

创建类型为T的对象的new表达式将初始化 对象如下:

A new-expression that creates an object of type T initializes that object as follows:

-如果省略new-initializer,则该对象为 默认初始化(8.5);如果未执行初始化,则 对象具有不确定的值.

— If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed,the object has indeterminate value.

-否则,新初始化程序为 根据8.5的初始化规则进行解释 直接初始化.

— Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.

在特定情况下,将对对象o执行默认初始化(即构造函数调用).但是他的子对象呢?似乎也执行了默认初始化.但是,在标准中,如果子对象的完整对象是默认初始化的,则在哪里指定对任何子对象执行默认初始化呢?

Default initialization (i.e. constructor call) is performed for object o in that particular case. But what about his subobjects? It seems that default initialization is performed too. But where does it specified in the Standard that default initialization is performed for any subobject if their complete object is default initialized?

推荐答案

说对对象o执行默认初始化是不正确的.在您的示例中,对象o被复制初始化.在您的示例中,默认初始化是对new创建的未命名对象执行的(此对象随后会泄漏).

It is not correct to say that default initialization is performed for object o. In your example object o is copy-initialized. Default initialization in your example is performed for the unnamed object created by new (which subsequently becomes leaked).

现在,由new创建的类型为O的未命名对象确实是默认初始化的,在这种情况下,这意味着它是通过调用用户定义的默认构造函数O::O()来初始化的. O::O()构造函数中的构造函数初始值设定列表完全不存在,即未提及任何子对象.这意味着这些子对象将被默认初始化.

Now, your unnamed object of type O created by new is indeed default initialized, which in this case means that it is initialized by a call to user-defined default constructor O::O(). The constructor initializer list in O::O() constructor is completely absent, i.e. does not mention any of the subobjects. That means that these subobjects will be default initialized.

12.6.2初始化基础和成员

8 在非委托构造函数中,如果给定的非静态数据成员或 基本类未由 mem-initializer-id 指定(包括 没有 mem-initializer-list 的情况,因为构造函数 没有 ctor-initializer ),并且该实体不是的虚拟基类 一个抽象类(10.4),然后

8 In a non-delegating constructor, if a given non-static data member or base class is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then

-如果实体是非静态数据 具有 brace-or-equal-initializer 的成员,则该实体为 已按照8.5中的规定进行初始化;

— if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;

-否则,如果实体是 变体成员(9.5),不执行初始化;

— otherwise, if the entity is a variant member (9.5), no initialization is performed;

-否则, 实体是默认初始化的(8.5).

— otherwise, the entity is default-initialized (8.5).

最后一个选项适用于您的情况. (由于我使用的是文档的草稿版本,因此编号可能已关闭.)

The last option applies to your case. (The numbering might be off since I'm using a draft version of the document.)

请注意,您问题的标题提到子对象初始化的顺序",而实际问题与顺序本身无关.这是关于初始化的方法.

Note that the title of your question mentions the "order of subobject initialization", while the actual question has nothing to do with the order per se. It is about the method of initialization.

这篇关于子对象初始化的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆