简体   繁体   English

初始化列表包含内部引用

[英]Initializer lists with internal references

I would like to use an initializer lists for object initialization to simplify object management, but the issue is that objects reference each other. 我想使用初始化列表进行对象初始化以简化对象管理,但问题是对象相互引用。

//B::B(A &a) //The only available constructor for B

class AB
{
    A m_a;
    B m_b;

    AB()
        : m_a()
        , m_b(m_a)

    ...
};

Is this allowed by standard? 这是标准允许的吗? From what I understand, it should be, given that member declaration within class are A, then B, order in initialization list doesn't matter, as they will be initialized by their physical order within class. 根据我的理解,应该是,鉴于类中的成员声明是A,那么B,初始化列表中的顺序无关紧要,因为它们将由它们在类中的物理顺序初始化。

Is this allowed by standard? 这是标准允许的吗? From what I understand, it should be, given that member declaration within class are A, then B, order in initialization list doesn't matter, as they will be initialized by their physical order within class. 根据我的理解,应该是,鉴于类中的成员声明是A,那么B,初始化列表中的顺序无关紧要,因为它们将由它们在类中的物理顺序初始化。

Yes, the order of initialization is that of the declaration of the member attributes in the class. 是的,初始化的顺序是类中成员属性声明的顺序。 Additionally, and depending on what B constructor does, it is correct (although close to the edge) to pass a reference to a yet uninitialized object as long as the reference (or pointer) as long as the reference or pointer is stored, but the object not used. 另外,根据B构造函数的作用,只要引用(或指针)存储引用(或指针),它就是正确的(虽然靠近边缘)传递对尚未初始化的对象的引用,但是对象未使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM