#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;
}
結果は次のとおりです。
0x7fff9920ee70
0x1036020
Foo destructor
0
20
10
Foo destructor
私は通常、リスト内のオブジェクトを消去した後、thar オブジェクトのメンバー変数にアクセスできなくなると考えています。しかし、上記では、何を指してc->m_a
いるオブジェクトを消去した後でもアクセスできます。なぜですか?c