我理解基本如何struct在C++中工作,如下例所示:
struct Options
{
int num_particles;
bool use_lbp;
string infile;
string outfile;
};
Run Code Online (Sandbox Code Playgroud)
但是,我不明白以下内容,在声明中还有一个附加部分.目的是Options():...什么?
struct Options
{
Options()
:num_particles(NUM_PARTICLES),
use_lbp(false),
infile(),
outfile()
{}
int num_particles;
bool use_lbp;
string infile;
string outfile;
};
Run Code Online (Sandbox Code Playgroud)
这与下面的代码中的内容类似吗?
struct State_
{
State_( State pp ) : p( pp ) { }
operator State() { return p; }
State p;
};
Run Code Online (Sandbox Code Playgroud)
Options()是你的默认构造函数.结构和类在它们可以具有方法和构造函数的意义上是等价的.之后:是初始化列表Options().它告诉你在你的默认构造函数中Options():
num_particles 初始化为 NUM_PARTICLESule_lbp 初始化为 falseinfile 初始化为空字符串outfile 初始化为空字符串类似的推理适用于State_.