Sunday, 15 September 2013

c++ - Initialising of class elements in vector -



c++ - Initialising of class elements in vector -

i have next code below. i'm wondering why code works , prints 5 lines of zero. shouldn't members of node uninitialised?

#include <vector> #include <iostream> struct node{ int a1; int a2; double b; double c; }; int main(){ std::vector<node> nodevec(5); for(auto s : nodevec){ std::cout << s.a2 << std::endl; // prints 5 lines of 0 } homecoming 0; }

edit:

just clarify question, according http://en.cppreference.com/w/cpp/container/vector/vector, states "3) constructs container count value-initialized (default constructed, classes) instances of t. no copies made." since node class, isn't default constructed? won't default construction of class node mean members uninitialised?

if read e.g. this std::vector constructor reference see items in vector initialized either node() or value-initialized. both of these equivalent pod types , value-initialize members in structure, i.e. set members zero.

c++ initialization

No comments:

Post a Comment