J
jimjim
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.
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.