#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
指向的对象后仍然可以访问,为什么?