C++11:class 初始化赋值顺序的主体
C++11: Body of class initialization assignment order
假设我有一个 class 形式
class MyClass
{
int a = 1;
int b;
int c;
int d = 4;
MyClass() : b(2)
{
c = 3;
}
}
a,b,c,d
的赋值顺序是什么?现在我确定(从旧的 C++ 中)b
将在 c
之前分配。但是对于后者,其他人呢?
来自[class.base.init]:
non-static data members are initialized in the order they were declared in the class definition
(again regardless of the order of the mem-initializers).
在这种情况下,排序等同于:
int a = 1;
int b = 2;
int c;
int d = 4;
c = 3;
假设我有一个 class 形式
class MyClass
{
int a = 1;
int b;
int c;
int d = 4;
MyClass() : b(2)
{
c = 3;
}
}
a,b,c,d
的赋值顺序是什么?现在我确定(从旧的 C++ 中)b
将在 c
之前分配。但是对于后者,其他人呢?
来自[class.base.init]:
non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
在这种情况下,排序等同于:
int a = 1;
int b = 2;
int c;
int d = 4;
c = 3;