when resizing a vector ,it will call constructor then destruct it.
struct CAT
{
CAT(){cout<<"CAT()"<<endl;}
CAT(const CAT& c){cout<<"CAT(const CAT& c)"<<endl;};
~CAT(){cout<<"~CAT()"<<endl;};
};
int main()
{
vector<CAT> vc(6);
cout<<"-----------------"<<endl;
vc.resize(3);
cout<<"-----------------"<<endl;
}
output:
$./m
CAT()
CAT(const CAT& c)
CAT(const CAT& c)
CAT(const CAT& c)
CAT(const CAT& c)
CAT(const CAT& c)
CAT(const CAT& c)
~CAT()
-----------------
CAT() //why resize will call constructor?
~CAT()
~CAT()
~CAT()
~CAT()
-----------------
~CAT()
~CAT()
~CAT()
I am using ubuntu 13.10 and gcc4.8
That's because of the optional argument for resize
.
This is the implementation I have in GCC 4.8:
void
resize(size_type __new_size, value_type __x = value_type())
{
if (__new_size > size())
insert(end(), __new_size - size(), __x);
else if (__new_size < size())
_M_erase_at_end(this->_M_impl._M_start + __new_size);
}
Take a closer look to value_type __x = value_type()
.
From http://www.cplusplus.com/reference/vector/vector/resize/:
void resize (size_type n, value_type val = value_type());
Before C++11, resize
had a defaulted second argument to provide a value for initialising new elements:
void resize(size_type sz, T c = T());
which explains why you see an extra object created and destroyed.
In a modern library, this is replaced with two overloads
void resize(size_type sz);
void resize(size_type sz, const T& c);
so you shouldn't see any extra objects unless you explicitly provide one. You should also see default-initialisation, rather than copy-initialisation, during construction.
It is possible that your implementation of vector::resize
creates a temporary default-initialized object even when downsizing, because it uses it to initialize new elements when it's upsizing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With