473,396 Members | 1,683 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,396 software developers and data experts.

simple destructor question

Hi All,

if I have a class that uses a STL list of pointers like:

class SomeClass {
private:
list<OtherClass* aList;
--other stuff--
}

If I just do aList.clear() in the destructor is that sufficient or do I have
to do a delete on each pointer in the list?

Thanks for your help

Michael
Apr 4 '07 #1
3 1397
michael wrote:
Hi All,

if I have a class that uses a STL list of pointers like:

class SomeClass {
private:
list<OtherClass* aList;
--other stuff--
}

If I just do aList.clear() in the destructor is that sufficient or do I have
to do a delete on each pointer in the list?
You have to delete each pointer. Maybe do it in SomeClass's destructor?
Apr 4 '07 #2
anon <an**@no.nowrote:
michael wrote:
>Hi All,

if I have a class that uses a STL list of pointers like:

class SomeClass {
private:
list<OtherClass* aList;
--other stuff--
}

If I just do aList.clear() in the destructor is that sufficient or do I have
to do a delete on each pointer in the list?

You have to delete each pointer. Maybe do it in SomeClass's destructor?
Or use a smart pointer of some sort:

class SomeClass {
private:
std::list< boost::shared_ptr< OtherClass aList;
};

aList.clear() will do The Right Thing. In fact, you don't even need to
call it in the dtor -- aList is destructed when the SomeClass object
goes away.
Keith
--
Keith Davies "Sometimes my brain is a very strange
ke**********@kjdavies.org to live in."
ke**********@gmail.com -- Dana Smith
http://www.kjdavies.org/
Apr 4 '07 #3
anon wrote:
michael wrote:
>Hi All,

if I have a class that uses a STL list of pointers like:

class SomeClass {
private:
list<OtherClass* aList;
--other stuff--
}

If I just do aList.clear() in the destructor is that sufficient or do
I have
to do a delete on each pointer in the list?

You have to delete each pointer. Maybe do it in SomeClass's destructor?
No, it depends what you intend. What we can say for sure is that
clearing (or destructing) the list will not do anything to the objects
pointed to by the contents of that list. Now if you actually intend to
delete each of those objects, then you need to do so explicitly yourself.
Apr 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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: Wellu Mäkinen | last post by:
Hi all, let's say I have a need to store 4 int values in somekind of a structure. I have atleast two options: class A { public: int a, b, c, b;
16
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body...
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
4
by: Joey | last post by:
Hi, I have come across codes like this if(dbConn != null) { dbConn.dispose(); } and sometimes like
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, I have a class A, which has three other members and each member is of a different class type. If I don't create a destructor then the compiler will give me one and whenever...
10
by: piboye | last post by:
Hi ! I'm a academician in china. I have been intereted in C++ lasting. In reading the C++ Primer book, i have a trouble about union. In the book ,it said that union can have constructors and...
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.