简体   繁体   English

vector.insert(...)是否会调用构造函数或赋值运算符?

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

I have a vector with elements of type class A . 我有一个类型为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). A有明确定义的拷贝构造函数,但复制操作已经取得了私人和不确定的(而不是指使用)。 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. vector确实使用了插入元素的复制构造函数。 However vector still needs the assignment operator internally, is within the requeriments for the vector type to be assignable. 但是, vector仍然需要内部的赋值运算符,在可以赋值的vector类型的requeriments中。 I believe C++11 now only requres move-assignment. 我相信C ++ 11现在只需要移动分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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