lists of objects..... and destructors....

S

SpreadTooThin

If you have a list of object like:

class image : public std::list<element>


Then does the element class need the copy constructor, operator=() and
the destructor methods implemented?

Seems to me that when the image is detroyed that the list is not
cleared...
Maybe this is why people are telling me I shouldn't inherit from
std::list....
 
S

SpreadTooThin

If you have a list of object like:

class image : public std::list<element>

Then does the element class need the copy constructor, operator=() and
the destructor methods implemented?

Seems to me that when the image is detroyed that the list is not
cleared...
Maybe this is why people are telling me I shouldn't inherit from
std::list....

Also when the list is cleared then the destructor of each object in
the list is supposed to be called as well right?
 
V

Victor Bazarov

If you're implementing in term of 'list', don't inherit publicly,
inherit privately and re-implement the functionality you need.

Depends. Does the list ever get sorted? Does it ever get any
insertions in it?

I would strongly doubt that. Any proof (code)?
Also when the list is cleared then the destructor of each object in
the list is supposed to be called as well right?

Yes.

V
 
J

James Kanze

If you have a list of object like:
class image : public std::list<element>
Then does the element class need the copy constructor, operator=() and
the destructor methods implemented?

I'm not sure what you mean by "implemented" here. In order to
instantiate a standard container, the contained type must have a
publicly accessible copy constructor, assignment operator and
destructor. (The last is so obvious, I'm not even sure the
standard mentions it explicitly. Without a publicly accessible
destructor, there's not much you can do with the type, except
derive from it.) It the type is a class type, and you don't
declare one of these, the compiler will declare it, and provide
the implementation. If you declare it, you must make it public,
and provide an implementation.
Seems to me that when the image is detroyed that the list is not
cleared...

Why? If the destructor of image is called, it will call the
destructor of its base class, and the destructor of std::list
will destruct any elements in the list at that time.
Maybe this is why people are telling me I shouldn't inherit from
std::list....

It's generally not a good idea to derive from one of the
standard containers, but this has nothing to do with it.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top