简体   繁体   English

构造函数初始化列表 - 这会有效吗?

[英]Constructor Initializer List - will this work?

With your help I could solve the problem I described in this minimal example ( use parameterized constructor in other classes constructor ) using a constructor initializer list. 在您的帮助下,我可以使用构造函数初始化列表解决我在此最小示例( 在其他类构造函数中使用参数化构造函数 )中描述的问题。

In my real life problem, things are a bit more difficult. 在我的现实生活中,事情有点困难。 My constructor now looks somehow like this: 我的构造函数现在看起来像这样:

ClassC::ClassC() : _objectB(_objectA.getInt()) {
  _objectA = ClassA();
}

Will this work? 这会有用吗? The compiler doesn't return any errors. 编译器不会返回任何错误。 However, _objectB is initialized with a value of _objectA , which is initialized in the line below. 但是, _objectB初始化为_objectA值,该值在下面的行中初始化。 So how would _objectA.getInt() return a valid value? 那么_objectA.getInt()将如何返回有效值?

If it doesn't work, how can I make it work? 如果它不起作用,我怎样才能使它工作?

If _objectA is declared before _objectB, then _objectA's default constructor will be called. 如果在_objectB之前声明了_objectA,那么将调用_objectA的默认构造函数。 And, _objectB will be initialzed with the getInt() value from _objectA. 并且,_objectB将使用_objectA中的getInt()值进行初始化。 Afterward, in the body of the constructor, _objectA is set via assignment operator with the default constructor of ClassA--which is unneeded. 之后,在构造函数的主体中,_objectA通过赋值运算符设置,使用ClassA的默认构造函数 - 这是不需要的。

If _objectB is declared before _objectA, then IDK...it may be compiler implementation dependent. 如果在_objectA之前声明_objectB,那么IDK​​ ......它可能依赖于编译器实现。

Either way, you are basically initializing _objectA twice. 无论哪种方式,您基本上都是初始化_objectA两次。

If _objectA is declared like 如果_objectA被声明为

class ClassC
{
    ClassA _objectA;
    ClassB _objectB;

    public:
    ClassC();
}

it will work, because _objectA is already initialized (with the default constructor), and you just reassign it a new class later, which has nothing to do with _objectB initialization. 它将起作用,因为_objectA已经初始化(使用默认构造函数),您稍后只需将其重新分配给新类,这与_objectB初始化无关。


However, as @πάντα ῥεῖ suggested it is better to use 但是,正如@πάνταῥεῖ建议使用它更好

ClassC::ClassC() : _objectA(), _objectB(_objectA.getInt()) {}

that will initialize _objectA , then use it for _objectB initialization. 这将初始化_objectA ,然后将其用于_objectB初始化。

So how would _objectA.getInt() return a valid value? 那么_objectA.getInt()将如何返回有效值?

Most probably yes. 很可能是的。 If objectA is initialized using the default constructor to yield valid values, it is already constructed implicitly (if the declaration appears before objectB ). 如果使用默认构造函数初始化objectA以生成有效值,则它已隐式构造(如果声明出现在objectB之前)。

The assignment in the constructor body is superfluous. 构造函数体中的赋值是多余的。

Also it's better to write: 还最好写:

 ClassC::ClassC() : _objectA(), _objectB(_objectA.getInt()) {}

to make that clear in the member initializer list. 在成员初始化列表中明确说明。

Class members are always initialized in the order they're declared, so assuming ClassC looks something like 类成员总是按照它们声明的顺序初始化,所以假设ClassC看起来像

class ClassC
{
    ClassA _objectA;
    ClassB _objectB;

public:
    ClassC();
//...
};

then your constructor is equivalent to 那么你的构造函数就相当于

ClassC::ClassC() : _objectA(), _objectB(_objectA.getInt()) {
    _objectA = ClassA();
}

As you can see, _objectA is initialized before _objectB and then later you copy-assign to _objectA from an anonymous ClassA object in the constructor's body. 如您所见, _objectA_objectB之前初始化,然后您从构造函数体中的匿名ClassA对象复制 - 赋值给_objectA

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

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