Considering this program:
考虑到这个项目:
#include <iostream>
class C
{
public:
C(void): a(1)
{ a=2; }
int a{3};
};
int main(void)
{
C c{};
std::cout << c.a; // 2
}
I can see three forms of data member initialization:
我可以看到数据成员初始化的三种形式:
- using a member initializer list
- 使用成员初始化器列表。
- using the Constructor
- 使用构造函数
- using a declaration in the class body
- 在类主体中使用声明
When to use which?
什么时候使用哪个?
2 个解决方案
#1
3
1: Using a declaration in the class body
1:在类主体中使用声明。
You should use this when the member will always be initialized with the same value, and it doesn't make sense to have to explicitly write that for each constructor.
当成员总是用相同的值初始化时,您应该使用这个方法,并且必须为每个构造函数显式地编写这个值是没有意义的。
2: Using a member initializer list
2:使用成员初始化列表
The member initializer list is obviously necessary for a member that lacks a default constructor, but aside from that, if you're initializing a member based on the constructor, it makes sense to do it here.
成员初始化器列表对于缺少默认构造函数的成员显然是必要的,但是除此之外,如果您正在基于构造函数初始化成员,那么在这里这样做是有意义的。
3: Using the constructor body
3:使用构造函数体
The constructor body is more useful for logic that can't be performed in a single statement (in the init-list). However, I don't think there is much difference between initializing a POD in the member initializer list or the constructor body.
构造函数体对于不能在单个语句(在init-list中)中执行的逻辑更有用。但是,我认为在成员初始化列表中初始化POD和构造函数主体之间没有太大的区别。
#2
2
My suggestion is to use:
我的建议是:
int a{3};
This makes sure that a
is initialized to 3
no matter how many constructors you have in the class.
这将确保无论类中有多少构造函数,a都被初始化为3。
My second choice will be to use the member initialization list.
我的第二个选择是使用成员初始化列表。
c(void) : a(1) {}
The third option, using code to set the value of a member variable, should be avoided.
第三个选项,使用代码来设置成员变量的值,应该避免。
#1
3
1: Using a declaration in the class body
1:在类主体中使用声明。
You should use this when the member will always be initialized with the same value, and it doesn't make sense to have to explicitly write that for each constructor.
当成员总是用相同的值初始化时,您应该使用这个方法,并且必须为每个构造函数显式地编写这个值是没有意义的。
2: Using a member initializer list
2:使用成员初始化列表
The member initializer list is obviously necessary for a member that lacks a default constructor, but aside from that, if you're initializing a member based on the constructor, it makes sense to do it here.
成员初始化器列表对于缺少默认构造函数的成员显然是必要的,但是除此之外,如果您正在基于构造函数初始化成员,那么在这里这样做是有意义的。
3: Using the constructor body
3:使用构造函数体
The constructor body is more useful for logic that can't be performed in a single statement (in the init-list). However, I don't think there is much difference between initializing a POD in the member initializer list or the constructor body.
构造函数体对于不能在单个语句(在init-list中)中执行的逻辑更有用。但是,我认为在成员初始化列表中初始化POD和构造函数主体之间没有太大的区别。
#2
2
My suggestion is to use:
我的建议是:
int a{3};
This makes sure that a
is initialized to 3
no matter how many constructors you have in the class.
这将确保无论类中有多少构造函数,a都被初始化为3。
My second choice will be to use the member initialization list.
我的第二个选择是使用成员初始化列表。
c(void) : a(1) {}
The third option, using code to set the value of a member variable, should be avoided.
第三个选项,使用代码来设置成员变量的值,应该避免。