jeudi 29 janvier 2015

Destructor never call

I don't understand why the destructor of ListELement is never call.

I use the class Base as counter, ListELement derive from Base to use the counter.


The Program:



#include <iostream>
#include <random>
#include <functional>

using namespace std;

class Base{
protected:
static int count;
};


template <class T>
class ListElement: public Base{

public:
ListElement(const T& value): next(NULL), data(value) { count++;}
~ListElement() { cout<<"dead:"<<count<<endl;}

//Setter
void SetData(const T& value) { data=value; }
void SetNext(ListElement* elem) { next = elem; }
//Getter
const T& GetData() const { return data; }
ListElement* GetNext() const { return next; }

private:
T data;
ListElement* next;
};

int Base::count = 0;

int main(){
random_device rd;
default_random_engine generator(rd());
uniform_int_distribution<int> distribution(1,100);
auto dice = bind(distribution, generator);

int nListSize = 1;
ListElement<int>* nMyList = new ListElement<int>(999);

ListElement<int>* temp = nMyList;//nMyList is the first element
for(int i=0; i<10; ++i) {
ListElement<int>* k = new ListElement<int>(dice()); //New element
temp->SetNext(k);
temp = temp->GetNext();
nListSize++;
}

temp=nMyList;
for(int i=0; i<nListSize; ++i){
cout<<"Value["<<i<<"]: "<<temp->GetData()<<endl;
temp = temp->GetNext();
}

return 0;
}


This is my output:



Value[0]: 999
Value[1]: 61
Value[2]: 14
Value[3]: 96
Value[4]: 51
Value[5]: 15
Value[6]: 37
Value[7]: 83
Value[8]: 1
Value[9]: 42
Value[10]: 95


If I type echo &? the console return my 0 so everything should be fine.


Aucun commentaire:

Enregistrer un commentaire