64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
int gv = 5;
double getDouble()
{
gv++;
const double& rd = gv;
double& ra = const_cast<double&>(rd);
ra *= 5;
return rd;
}
struct A
{
char c;
int i;
double d;
public:
A() : d(getDouble()), i(gv++) {}
};
A a;
int main()
{
cout << a.c + 3 << endl;
cout << a.i << endl;
cout << a.d;
}
5 Initialization shall proceed in the following order:
— First, and only for the constructor of the most derived class as described below, virtual base classes shall
be initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph
of base classes, where “left-to-right” is the order of appearance of the base class names in the derived
class base-specifier-list.
— Then, direct base classes shall be initialized in declaration order as they appear in the base-specifier-list
(regardless of the order of the mem-initializers).
— Then, nonstatic data members shall be initialized in the order they were declared in the class definition
(again regardless of the order of the mem-initializers).
— Finally, the body of the constructor is executed.