Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?
From: Rob Williscroft (rtw_at_freenet.co.uk)
Date: 04/11/04
- Next message: Leor Zolman: "Re: std::vector - bug or feature?"
- Previous message: Jeff Schwab: "Re: Programms memory adress location access?"
- In reply to: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Next in thread: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Reply: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Apr 2004 15:08:57 GMT
jimjim wrote in news:Y3dec.1164$FT7.11504891@news-text.cableinet.net in
comp.lang.c++:
> I would appreciate helping me understand why erase doesnt delete the
> object when I store a pointer to the object.
>
> Thanks in advance
>
>
#include <iostream>
#include <ostream>
#include <list>
using namespace std;
class X
{
public:
X(){ cout << "X constructor\n"; }
~X() { cout << "X destructor\n"; }
X( X const & ) { cout << "X copy constructor\n"; }
};
int main()
{
X x;
list<X>lists;
lists.push_back(x);
lists.erase(lists.begin());
}
output:
X constructor
X copy constructor
X destructor
X destructor
Note 2 constructors followed by 2 destructors:
The 1st ctor is for local 'x'.
The 2nd ctor is for the *copy* that is pushed into 'lists'.
The first dtor is for the X erased by erase( lists.begin() ).
The last dtor is for local 'x'.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
- Next message: Leor Zolman: "Re: std::vector - bug or feature?"
- Previous message: Jeff Schwab: "Re: Programms memory adress location access?"
- In reply to: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Next in thread: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Reply: jimjim: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|