By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Do I need to use destructor here?

P: n/a
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? */
}

Thanks,
Pushkar Pradhan

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 22 '05 #2

P: n/a


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?

Jul 22 '05 #3

P: n/a
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.

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.