thought I understood scope... guess not
I create items in a while loop:
MyStruct *temp = new MyStruct;
I add some data and then I put the items in a vector with much higher scope:
MyVector.push_back(*temp);
For a single varaible this works find but when I add the second variable the first varaible's destructor is called. I thought that if a variable is created as a pointer and new is used to allocate it, its destructor isn't call until I explicitly call delete or MyVector.clear().
What am I missing?
Re: thought I understood scope... guess not
Quote:
Originally posted by pim42
I create items in a while loop:
MyStruct *temp = new MyStruct;
I add some data and then I put the items in a vector with much higher scope:
MyVector.push_back(*temp);
For a single varaible this works find but when I add the second variable the first varaible's destructor is called. I thought that if a variable is created as a pointer and new is used to allocate it, its destructor isn't call until I explicitly call delete or MyVector.clear().
What am I missing?
Please post all the code that shows your problem. I don't see a while() loop, and I don't see a call to vector.clear(). Also, vector.clear() does not and cannot call delete for your pointer. How does the vector know that the pointer you placed in it was dynamically allocated? You could have done this:
Code:
MyStruct t;
MyVector.push_back(&t);
Regards,
Paul McKenzie