简体   繁体   English

初始化列表与初始化方法

[英]Initializer list vs. initialization method

There are at least two ways to initialize a class in C++. 至少有两种方法可以在C ++中初始化一个类。

(1) Initializer List (1)初始化列表

struct C
{
  int i;
  C() : i(0) {}
};

(2) Initializer Method (2)初始化方法

struct D
{
  int i;
  C() { init(); }
  void init() {
    i = 0;
  }
};

I need to re-init objects of my class from time to time. 我需要不时地重新启动我班级的对象。 With the second solution, I can simply call obj.init() . 使用第二个解决方案,我可以简单地调用obj.init() With the first solution, I would either have to add an init() function which essentially duplicates the initializer list effect or use obj = C() . 使用第一个解决方案,我要么必须添加一个init()函数,它基本上复制了初始化列表效果或使用obj = C()

Is there a more-or-less consensus on what variant is better here? 对于哪种变体在这里更好,是否存在或多或少的共识? Is there a disadvantage to using an initializer method (except the possible loss of performance as mentioned in the C++ FAQ ). 使用初始化方法是否有缺点(除了C ++ FAQ中提到的可能的性能损失)。

The main difference is that without using initialization list, members are created and then values are assigned to them. 主要区别在于,如果不使用初始化列表,则会创建成员,然后为其分配值。 When you use initialization list, members are directly created by using given values. 使用初始化列表时,可以使用给定值直接创建成员。

One of situations, when using initialization is important, is when your class holds some references as a members and it is necessary to initialize these members right when they are being constructed: 在使用初始化时,其中一种情况很重要,就是当您的类将某些引用作为成员保存时,有必要在构造它们时正确初始化这些成员:

class A
{
public:
    A(B& bRef) : bRef_(bRef) { }
private:
    B& bRef_;   
}

This question could help you too: In this specific case, is there a difference between using a member initializer list and assigning values in a constructor? 这个问题也可以帮到你: 在这个特定的情况下,使用成员初始化列表和在构造函数中赋值是否有区别?

Your init() method is perfectly fine. 你的init()方法非常好。 As you yourself have mentioned, you want to initialize these members more times than just first time when the instance of this class is being constructed, so for the sake of reusability and simplicity it's right to keep it in a method. 正如您自己提到的那样,您希望初始化这些成员的次数多于第一次构造此类实例的时间,因此为了可重用性和简单性,将其保留在方法中是正确的。 Don't try to improve performance of your code unless you really need it. 除非您确实需要,否则不要尝试提高代码的性能。

Some people say that It's easier to make a correct program fast than it's to make a fast program correct. 有人说, 快速制作正确的程序比制作快速程序更容易。 ;) ;)

When creating an array (using vector, or allocating dynamically using new ) you will have to explicitly call init on each of its members while using a constructor, it will automatically be called for all elements. 在创建数组(使用向量或使用new动态分配)时,您必须在使用构造函数时在其每个成员上显式调用init ,它将自动为所有元素调用。

I prefer placing basic initialization into the constructor and more complex logic into an init method. 我更喜欢将基本初始化放入构造函数中,将更复杂的逻辑放入init方法中。 In my opinion a constructor should not perform any complex operations. 在我看来,构造函数不应该执行任何复杂的操作。

Below are the scenarios when initializer list is used: 以下是使用初始化程序列表时的方案:

  • For initialization of non-static const data members. 用于初始化非静态const数据成员。
  • For initialization of reference members. 用于初始化参考成员。
  • For initialization of member objects which do not have default constructor. 用于初始化没有默认构造函数的成员对象。
  • For initialization of base class members. 用于初始化基类成员。
  • When constructor's parameter name is same as data member. 当构造函数的参数名称与数据成员相同时。
  • For Performance reasons. 出于性能原因。

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

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