简体   繁体   中英

Does vector.insert(…) call the constructor or the assignment operator?

I have a vector with elements of type class A . Class A has an explicitly defined copy constructor, but the copy operator has been made private and undefined (it's not meant to be used). If I insert new elements to the vector like below

A walker;                      //This calls the default constructor.
std::vector< A > vec;
std::vector< A >::iterator it = vec.begin();
vec.insert( it, walker );      //Shouldn't this call the copy constructor?

I get an error complaining that the copy operator is private. But shouldn't vector be using the copy constructor?

vector is indeed using the copy constructor for the inserted element. However vector still needs the assignment operator internally, is within the requeriments for the vector type to be assignable. I believe C++11 now only requres move-assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM