views:

112

answers:

2

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
***desctructor
---constructor
***desctructor
---constructor
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor
------------------------------------
***desctructor
***desctructor
***desctructor
***desctructor
***desctructor

I do not understand why so many destructors are called.

+15  A: 

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.

Charles Bailey
+1: Implicit copies are the cause.
DeadMG
+1  A: 

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()

Vladimir
@Vladimir Your answer helped a lot. I reserve space now wherever possible, not only vectors. My program runs a lot faster. Thanks!
Xolve