gpt4 book ai didi

c++ - C++隐式复制构造函数成员变量复制顺序

转载 作者:行者123 更新时间:2023-12-02 09:49:32 24 4
gpt4 key购买 nike

只是想仔细检查一下:C++标准保证成员变量由隐式复制构造函数按声明的顺序复制,对吗?在下面的示例中,ab之前复制,对吗? (假设AB都具有非平凡的复制构造函数,并且对于正确性来说,在复制a之前复制b是很重要的。)

struct Foo {
A a;
B b;
};

我对它是否由标准保证还是依赖于实现感兴趣?

最佳答案

是的,可以保证implicitly-defined copy constructor的初始化顺序:

For non-union class types (class and struct), the constructor performs full member-wise copy of the object's bases and non-static members, in their initialization order, using direct initialization.



data members的初始化顺序是其声明的顺序。

3) Then, non-static data members are initialized in order of declaration in the class definition.



从标准 [class.copy.ctor]/14

(强调我的)

The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: Default member initializers of non-static data members are ignored. See also the example in [class.base.init]. — end note ] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor (see [class.base.init]).



[class.base.init]/13.3

Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).

关于c++ - C++隐式复制构造函数成员变量复制顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61357266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com