开发者

C++ Too many destructors called so few objects [duplicate]

开发者 https://www.devze.com 2023-01-14 02:30 出处:网络
This question already has answers here: Two calls to destructor (3 answers) Closed 8 years ago. Here is the code (also at http://pastebin.com/yw5z2hnG ):
This question already has answers here: Two calls to destructor (3 answers) Closed 8 years ago.

Here is the code (also at http://pastebin.com/yw5z2hnG ):

#include <iostream>
#include <vector>
using namespace std;

class X
{
    public:
    int i;
    X();
    ~X();
};

X::X()
{
    i = 1;
    cout << "---constructor" << '\n';
}

X::~X()
{
    cout << "***desctructor" << '\n';
}

int main()
{
    vector<X> *vx = new vector<X>;
    cout << "------------------------------------" << endl;
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    vx->push_back(X());
    cout << "------------------------------------" << endl;
    delete vx;
}

I get the output as:

------------------------------------
---constructor
***desctructor
---constructor
***desctructor
***desctructor
---constructor
***desctructor
***desctructor
***desct开发者_运维技巧ructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor

I do not understand why so many destructors are called.


If you define your own copy constructor you will see the other objects being constructed:

class X
{
    public:
    int i;
    X(const X&);
    X();
    ~X();
};

X::X(const X& x) : i( x.i )
{
    cout << "---copy constructor\n";
}

// ... rest as before

The compiler will provide a copy constructor that performs no logging if you don't declare your own one.


One more detail - if you reserve space for your vector in advance:

int main()
{
    vector<X> *vx = new vector<X>;
    vx->reserve(5);
    ....

Then you'll get the following output:

---constructor
+++ copy constructor
***desctructor
---constructor
+++ copy constructor
***desctructor
...

So as you can see vector also copies all its elements when it is needed to reallocate its storage - that's why you get 5 copy constructors and destructors for the last push_back()

0

精彩评论

暂无评论...
验证码 换一张
取 消