WAS: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?
From: jimjim (FreeBSD_net_at_blueyonder.co.uk)
Date: 04/14/04
- Next message: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Previous message: cedric buerfent: "Re: What's wrong with this function"
- Next in thread: Jeff Flinn: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Reply: Jeff Flinn: "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: Wed, 14 Apr 2004 14:28:58 GMT
Hello again,
Consider the following code:
#include <iostream>
#include <list>
class X {
private:
int val;
public:
X(int v){ cout << "X constructor\n"; val = v;}
~X() { cout << "X destructor\n"; }
X( X const & ) { cout << "X copy constructor\n"; }
int return_val() {return val;}
};
int main(){
X x(6);
list<X>lists;
lists.push_back(x);
list<X>::iterator i = lists.begin();
cout << (*i).return_val()<<endl;
lists.erase(lists.begin());
}
The output is as expected:
X constructor
X copy constructor
0
X destructor
X destructor
Why is 0 returned by the return_val() and not 6? I assume when I push the x
object into the list, I copy both the object and the state.
- Next message: Leor Zolman: "Re: converting 1944 to '1','9','4','4'"
- Previous message: cedric buerfent: "Re: What's wrong with this function"
- Next in thread: Jeff Flinn: "Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?"
- Reply: Jeff Flinn: "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
|