Net*_*ave 4 c++ struct initialization
我最近碰到过这个:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm> //sort function
#include <functional> //functions utilities
#include <random> //random numbers generation
using namespace std;
default_random_engine generator;
uniform_int_distribution<int> distribution(0,9999);
auto randomer = bind(distribution, generator);
struct Test_struct {
string ord_as_string;
int ord;
Test_struct() :
ord(randomer()),
ord_as_string(to_string(ord))
{}
};
int main() {
vector<Test_struct> my_vector;
for(int i = 0; i < 100; ++i) my_vector.push_back(Test_struct());
for(auto& e: my_vector)
cout << e.ord_as_string << " -> " << e.ord << endl;
}
Run Code Online (Sandbox Code Playgroud)
Wich打印以下序列:
142 -> 0
0 -> 1315
1315 -> 7556
7556 -> 4586
4586 -> 5327
5327 -> 2189
2189 -> 470
470 -> 6788
6788 -> 6792
...
Run Code Online (Sandbox Code Playgroud)
它没有意义,因为struct int和stringattributes应该引用相同的逻辑数字.所以我只是将声明改为:
struct Test_struct {
int ord;
string ord_as_string;
Test_struct() :
ord(randomer()),
ord_as_string(to_string(ord))
{}
};
Run Code Online (Sandbox Code Playgroud)
我认为它有一些东西来处理struct属性的声明顺序,它起作用:
0 -> 0
1315 -> 1315
7556 -> 7556
4586 -> 4586
5327 -> 5327
2189 -> 2189
470 -> 470
6788 -> 6788
...
Run Code Online (Sandbox Code Playgroud)
那么,有人可以向我解释为什么引用将ord保留在以下结构初始化中,当它在另一个属性之后声明但在之前初始化之前?
编辑:好的,成员按照声明中的说明初始化,然后,为什么它的设计是这样的?这对我来说毫无意义.
您正在遇到未定义的行为.成员的初始化顺序必须与声明的顺序相匹配.
因为它没有,你观察到的行为源于ord成员未初始化并且在循环的每次迭代中巧合地重用内存中的相同位置.此位置恰好保存您之前在迭代中指定的值.
您可以在这里阅读初始化的顺序,以及为什么会这样.总结链接和注释:析构函数必须以初始化的相反顺序销毁对象,并且因为可以有多个构造函数,所以必须有一个单一的事实源,这是类中声明的顺序.