i've declared struct this:
struct point{ double x,y; point(){} point(double xx,double yy): x(xx),y(yy){} };
and i'm trying fill vector this:
vector<point> s(1); point tmp(1,2); s[0]=tmp; s.push_back(s[0]);
unfortunately, doing gives me wrong answer when display content of s[1], s.push_back((point)s[0])
leads expected answer (e.g. copy of s[0]).
what difference between s.push_back(s[0])
, s.push_back((point)s[0])
explains differennce ?
is there auto type conversion or going under hood?
push_back
take argument const reference (and other overload rvalue reference), so
s.push_back(s[0]);
uses lvalue-reference of first element, but, push_back
may reallocate if capacity not large enough. , reference pulled s[0]
left dangling, , no longer viable.
on other side
s.push_back((point)s[0]);
create temporary, , s[0]
no longer used, allowing push valid point
in vector
.
Comments
Post a Comment