Pushkar Pradhan wrote:
Gianni Mariani wrote:
Pushkar Pradhan wrote:
I have a function which holds a list of my data structure PARTICLE,
initially the list is declared empty, but I grow it in this function.
Now my question is do I have to write a destructor (since the
datatype is my own) to delete the list?
Or does it get deleted automatically once the function exits?
Code:
compute_hull(....)
{
list<PARTICLE> hull2;
for(i = 0; i < X; i++) {
/* depending on some condition */
hull2.push_back(...);
}
/* is destructor reqd. for hull2 now? */
}
hull2 is a variable. list<> has a destructor that will delete all the
"PARTICLE" objects. I have no idea if PARTICLE requires a destructor.
G
I tried doing a "~hull2()", but the compiler gives syntax error, does
that help?
The destructor call would be: hull2.~list<PARTICLE> but this would be
*really bad* to call because it's automagically called for you when you
return (or take an exception and the stack is rolled back past a
compute_hull stack frame).
Rule - don't go calling destructors unless you're doing your own memory
management (i.e. writing your own container). Use the language
semantics to do this for you.