c++ - push_back object reference -
as saw code, wondering run without problems.
the object passed reference isn't copy of it. if put reference vector , object out of scope, shouldn't accessible anymore.
but does. reason why works push_back()
creates copy of referenced object. answer of behavior?
struct struct1 { int value; }; std::vector<struct1> testvect; void pushvect(struct1 & element) { testvect.push_back(element); } void fillvect() { struct1 s1; (int = 0; < 10; i++) { s1.value = i; pushvect(s1); } }
the push_back
methods of vector taking care of making copy itself: c++ reference
adds new element @ end of vector, after current last element. content of val copied (or moved) new element.
so during call
estvect.push_back(element);
the vector works copy. therefore there no problem.
Comments
Post a Comment