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

Discussion in 'C++' started by SpreadTooThin, Apr 4, 2007.

  1. 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....
    SpreadTooThin, Apr 4, 2007
    #1
    1. Advertising

  2. On Apr 4, 1:33 pm, "SpreadTooThin" <> wrote:
    > 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?
    SpreadTooThin, Apr 4, 2007
    #2
    1. Advertising

  3. SpreadTooThin wrote:
    > On Apr 4, 1:33 pm, "SpreadTooThin" <> wrote:
    >> If you have a list of object like:
    >>
    >> class image : public std::list<element>


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

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


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

    >> Seems to me that when the image is detroyed that the list is not
    >> cleared...


    I would strongly doubt that. Any proof (code)?

    >> 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?


    Yes.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 4, 2007
    #3
  4. SpreadTooThin

    James Kanze Guest

    On Apr 4, 9:33 pm, "SpreadTooThin" <> wrote:
    > 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.

    --
    James Kanze, Apr 5, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joe martin
    Replies:
    3
    Views:
    374
    Francis Glassborow
    Jul 16, 2003
  2. gorda
    Replies:
    5
    Views:
    5,031
    akhyls
    Mar 7, 2010
  3. Replies:
    10
    Views:
    2,341
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    392
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Ravi
    Replies:
    2
    Views:
    268
Loading...

Share This Page