Re: why it seems that std::list::erase() doesnt free objects in a list, if the latter holds pointers to them?

From: Jeff Flinn (NONONE_at_nowhere.com)
Date: 04/14/04


Date: Wed, 14 Apr 2004 10:35:16 -0400


"jimjim" <FreeBSD_net@blueyonder.co.uk> wrote in message
news:KUbfc.3426$Nw.32632966@news-text.cableinet.net...
> 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.

Given X's copy constructor, why would you expect it to be 6?

Jeff F



Relevant Pages