class A {
public:
int _a;
A() : _a(2) {} // 构造函数初始化列表
}
The effect is the same. The process of assigning initial values will occur in the member initialization phase of the constructor. The former is just syntactic sugar added by C++11. In this way, if a class has multiple constructors and the initial value of a member is the same in these constructors, then the initial value can be set through in-class initialization to reduce duplication of work.
This is more convenient because you can use type var=? or type var{} assignment directly when declaring, which can get rid of the trouble of writing a constructor for initialization, but this requires the latest c++11 standard, compared to Initialization list, you no longer need to adjust the order of variables in the initialization list in order to get rid of the compiler's warning that the initialization and declaration order are inconsistent (which is sometimes very dangerous).
struct Init
{
int x = 11;
int z {10};
};
Initialization list
The initialization list is a very "old" initialization method. It has obvious advantages over in-place initialization. It does not require any new feature support. All versions of C++ can support it. Another advantage is that it can be used委托构造 (c++11).
struct InitList
{
InitList()
:InitList(10, 11)
{}
InitList(int x, int z)
:x(x)
,z(z)
{}
int x;
int z;
};
As for what to use is a matter of personal preference, the initialization list is more general, and currently the two can also be used together.
Note: When a member exists in both the initialization list and in-place initialization, the initialization list will be executed first, and the in-place initialization will be ignored.
The initial value within the class will be transcribed into the constructor So there is no difference Since the initialization order of members with the initial value within the class will still be in the default order, I recommend not writing a class Internal initial value
This way of writing is the same as
The effect is the same. The process of assigning initial values will occur in the member initialization phase of the constructor. The former is just syntactic sugar added by C++11. In this way, if a class has multiple constructors and the initial value of a member is the same in these constructors, then the initial value can be set through in-class initialization to reduce duplication of work.
In-place initialization ("direct assignment") is better, concise and clear
I guess you want to assign _a to 2 when each class is initialized, right?
In this case, just write int _a = 2 into the default constructor of A.
Or do you want multiple instances of a class to share one _a variable?
In this case you should use the static keyword
Also pay attention to access permissions
In-place initialization
This is more convenient because you can use
type var=?
ortype var{}
assignment directly when declaring, which can get rid of the trouble of writing a constructor for initialization, but this requires the latestc++11
standard, compared to Initialization list, you no longer need to adjust the order of variables in the initialization list in order to get rid of the compiler's warning that the initialization and declaration order are inconsistent (which is sometimes very dangerous).Initialization list
The initialization list is a very "old" initialization method. It has obvious advantages over in-place initialization. It does not require any new feature support. All versions of C++ can support it. Another advantage is that it can be used
委托构造
(c++11).As for what to use is a matter of personal preference, the initialization list is more general, and currently the two can also be used together.
Note: When a member exists in both the initialization list and in-place initialization, the initialization list will be executed first, and the in-place initialization will be ignored.
The initial value within the class will be transcribed into the constructor
So there is no difference
Since the initialization order of members with the initial value within the class will still be in the default order, I recommend not writing a class Internal initial value