Why should I prefer to use member initialization lists?

后端 未结 9 1261
醉酒成梦
醉酒成梦 2020-11-21 04:43

I\'m partial to using member initialization lists with my constructors... but I\'ve long since forgotten the reasons behind this...

Do you use member initialization

9条回答
  •  甜味超标
    2020-11-21 05:24

    Before the body of the constructor is run, all of the constructors for its parent class and then for its fields are invoked. By default, the no-argument constructors are invoked. Initialization lists allow you to choose which constructor is called and what arguments that constructor receives.

    If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.

提交回复
热议问题