Constructors, destructors and pointers (and vectors and arrays and delete and that)
===

Funky title, but honestly I couldn't think of anyone better, sorry :(

While experimenting with pointers I came across this and I need help understanding it. Basically, I create a vector of a pointer to an object.  When deleting the pointer that's in the vector, I expect the original ovject to be deleted as well. They are one and the same no?

Here's what I think I do in the code that follows I create a dynamically allocated array, I then put the pointer to half of the elements of the array in a vector of pointers. Each step of the way every object of the class testDestructor knows which 'order' it was created in, they all get assigned an incrementing number via a static integer.

I then pop_back and delete all the pointers in the vector. I only use pop_back because I wanted to see if they get deleted by the vector class, apperantly they don't, but idk if I'm missing something there. The important bit is I delete it.

Now, what I expect to happen is that this deletes the corresponding elements of the array as well. This is what SHOULD happen, because the vector and the array element point to the same place in the memory. ie, the destructor should get called.

So when I then delete the array, I expect that either only 5 of the elements get deleted, or a run-time error occurs (I've had this happen before when I try deleting pointers that already are deleted, but that might have been a different scenario idk). Namely, I expect that only the destructor should only be called five times.

HOWEVER, that's not what happens. The constructor gets called 10 times but the destructor gets called 15 times. What am I missing? Am I missing a constructor (I only know about default constructors and copy constructors, are there more), or is it something else? Because, in my mind, when the destructor is gone the object is gone.

Sorry if it's too much code:

testDestructor.h:

    #ifndef TESTDESTRUCTOR_H
    #define TESTDESTRUCTOR_H
 
    class testDestructor
    {
    public:
       testDestructor();
       testDestructor(const testDestructor &);
       ~testDestructor();

       static int maxTest;
       int test;
    };

    #endif

testDestructor.cpp:

    #include "testDestructor.h"
    #include <iostream>

    using namespace std;

    int testDestructor::maxTest = 0;

    testDestructor::testDestructor()
    {
       test = maxTest++;
       cout << "testDestructor nr " << test << " created successfully\n";
    }

    testDestructor::testDestructor(const testDestructor &destructorToCopy)
    {
       test = maxTest++;
       cout<< "testDestructor nr " << test << " created successfully (using the copy constructor\n";
    }
    testDestructor::~testDestructor()
    {
      //maxTest--;
       cout << "testDestructor " << test << " destroyed successfully\n";
    }

main.cpp:

    #include <iostream>
    #include "testDestructor.h"
    #include <vector>
    using namespace std;

    int main()
    {
       cout << "   creating pointer array:\n\n";
       testDestructor *testPtr = new testDestructor[10];

       cout << "   intitiating vector\n\n";
       vector<testDestructor*> testVct;


       for (int i = 0; i < 5; i++)
       {

          cout << "   pushing back vector " << i << ":\n";
          testVct.push_back(testPtr + i);
          cout << "testDestructor " << testVct[i]->test << " pushed back\n";
       }
       cout << "\n";

       for (int i = 4; i >= 0; i--)
       {
          cout << "   popping back vector " << i << ":\n";
          cout << "testDestructor " << testVct[i]->test << " popped back\n";

          delete testVct[i];
          testVct.pop_back();

       }
       cout << "\n";

       cout << "   deleting pointer array\n\n";
       delete [] testPtr;
    }
