473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Do I need to use destructor here?

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
3 1479
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jimmy Johns | last post by:
Hi all, I have some class hierarchy as follows: class S {}; class A {}; class B {public: vector<S*> v_; virtual ~B();}; class C : public virtual A, public virtual B { // do I need to define...
52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
by: | last post by:
Hi, From 11.11 here, I know that member objects get their dtor's called autmatically: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.11 What if I have a pointer to a member object?...
2
by: arun | last post by:
Hello Group, The compiler generated destructor will invoke destructor of each member of the containing class let us say 'class A'. However, if I write my own destructor for class A like; ...
3
by: yancheng.cheok | last post by:
Hello all, I try to figure out what is the sequence when we create and delete an object. After experiment on my VC++ 2003, I found that here is the sequence new-constructor-destructor-delete ...
7
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the...
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
12
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.