#include <list>
#include <iostream>
struct Foo
{
Foo(int a):m_a(a)
{}
~Foo()
{
std::cout << "Foo destructor" << std::endl;
}
int m_a;
};
int main( )
{
std::list<Foo> a;
Foo b(10);
std::cout << &b << std::endl;
a.push_back(b);
Foo* c = &(*a.begin());
std::cout << c << std::endl;
a.erase(a.begin());
std::cout << a.size() << std::endl;
c->m_a = 20;
std::cout << c->m_a << std::endl;
std::cout << b.m_a << std::endl;
}
The result is:
0x7fff9920ee70
0x1036020
Foo destructor
0
20
10
Foo destructor
I usually think after i erase an object in a list i can't access member variable of thar object any more. But in the above I can still access c->m_a
after I have erased the object what c
points to,Why?