S
Siam
Hi,
I'm a little new to stl so bear with me...Say I have the following
code:
vector<int> vec;
int i = 3;
vec.push_back(i);
i=4;
cout<<vec.at(0)<<endl;
Looking at the signature of push_back, it seems to take a reference:
void push_back( const TYPE& val );
I wouldve thought that means the int i pass to the vector isn't
copied, but a reference to the original int is placed in the vector.
However, the above code returns 3, not 4, indicating the int has been
copied into the vector (on printing memory addresses, the ints are
stored in different memory addresses). My question is, why does the
push_back function take a reference, but yet still store a copy of the
object in the vector?
Cheers,
SR
I'm a little new to stl so bear with me...Say I have the following
code:
vector<int> vec;
int i = 3;
vec.push_back(i);
i=4;
cout<<vec.at(0)<<endl;
Looking at the signature of push_back, it seems to take a reference:
void push_back( const TYPE& val );
I wouldve thought that means the int i pass to the vector isn't
copied, but a reference to the original int is placed in the vector.
However, the above code returns 3, not 4, indicating the int has been
copied into the vector (on printing memory addresses, the ints are
stored in different memory addresses). My question is, why does the
push_back function take a reference, but yet still store a copy of the
object in the vector?
Cheers,
SR