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


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.



Relevant Pages

  • Re: Deleting files modified before a specified number of days using Java.
    ... Not even able to create the fileArray object.... ... The class is doing too much work in the constructor - the constructor should only construct, ... // members should be private! ... public FileDeleter(String path, int days) ...
    (comp.lang.java.programmer)
  • Re: Two questions about...something
    ... > int value; ... that constructor is so trivial it does nothing. ... If there were more than one member, then their constructors, even if they do ...
    (comp.lang.cpp)
  • Re: help with classes
    ... To make it clear to the compiler that this is a constructor that does not ... int main ... standalone functions into calls to member functions, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)
  • FAQ Suggestions
    ... It can only be applied to reference type variables converting to ... The .NET runtime can't guarantee that parameterless constructors will be ... performing by not having to call constructor code. ... number like casting an int to short) are always explicit. ...
    (microsoft.public.dotnet.languages.csharp)