简体   繁体   English

c ++入门练习13.13,关于构造函数

[英]the c++ primer exercise 13.13, about the constructor

i did the exercise 13.13 of c++ primer, and here is my code 我做了c ++入门的练习13.13,这是我的代码

#include <iostream>
#include <vector>

using namespace std;

struct X{
X(){cout<<"X()"<<endl;}
X(const X&){cout<<"X(const X&)"<<endl;}
X& operator=(const X&){
    cout<<"X& operator=(const X&)"<<endl;
    return *this;
}
~X(){cout<<"~X()"<<endl;}
};

void foo(const X& cx, X bx){
vector<X> v;
v.push_back(cx);
v.push_back(bx);
}

int main(){
X* x1=new X;
X x2;
x2=*x1;
foo(*x1, x2);
delete x1;
return 0;
}

and I got the output which has four times of "X(const X&)", I guess the first three comes from passing x2, push_back(cx) and push_back(bx). 我得到的输出是“ X(const X&)”的四倍,我猜前三个来自传递x2,push_back(cx)和push_back(bx)。 but why there is the fourth "X(const X&)? 但是为什么会有第四个“ X(const X&)”?

Possibly when the second element is added to the vector (the v.push_back(bx); ) a resize of the vector's underlying storage is done and the single element already in the vector is copied to the new storage. 可能在将第二个元素添加到向量( v.push_back(bx); )时,将对向量的基础存储进行调整大小,并将向量中已存在的单个元素复制到新的存储中。

Add the following around the second push_back() call: 在第二个push_back()调用周围添加以下内容:

cout << "about to do push_back(bx)" << endl;
v.push_back(bx);
cout << "done push_back(bx)" << endl;

It has something to do with the way push_back is implemented. 它与push_back的实现方式有关。

One of the copies is due to passing bx to foo by value. 副本之一是由于按值将bx传递给foo Make that parameter a reference, and the number of copies reduces by one. 将该参数作为参考,副本数减少一。

The rest appear to all be due to push_back . 其余的似乎都是由于push_back I tried your code at http://www.tutorialspoint.com/compile_cpp_online.php with extra printouts before and after every push_back . 我在http://www.tutorialspoint.com/compile_cpp_online.php上尝试了您的代码,并在每次push_back之前和之后都有了额外的打印输出。 I also pushed a total of 10 elements onto the vector. 我还总共将10个元素推到向量上。 The second call to push_back caused the copy constructor to be called twice, the third call caused it to be called three times, the fifth call caused five times, and the ninth call caused nine times. push_back的第二次调用导致复制构造函数被调用两次,第三次调用使它被调用三次,第五次调用导致五次,第九次调用导致九次。 All other calls to push_back caused the copy constructor to be called only once. 所有其他对push_back调用都会导致复制构造函数仅被调用一次。

But when I did v.reserve(20) before the first call to push_back , every call to push_back resulted in exactly one call to the copy constructor. 但是,当我在第一次调用push_back之前执行v.reserve(20) ,每次对push_back调用都会导致对复制构造函数的一个调用。 When I changed the 20 to 8, the ninth push_back resulted in nine calls to the copy constructor, all others resulted in just one call. 当我将20更改为8时,第九个push_back导致对副本构造函数的九次调用,所有其他结果仅导致一个调用。

I conclude that the extra copy constructor calls are related to the maintenance the vector has to do (copying existing elements) when it grows without having "reserved" the space it requires. 我得出的结论是,额外的复制构造函数调用与vector增长时必须进行的维护(复制现有元素)有关,而无需“保留”所需的空间。

So Michael Burr's hypothesis is correct, and you should be able to confirm this too by experimenting with the code. 因此,迈克尔·伯尔(Michael Burr)的假设是正确的,您也可以通过试验代码来确认这一点。

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

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