我运行此代码段来测试问题:
#include <iostream>
#include <functional>
using namespace std;
class A
{
private:
int i;
public:
A(): i(0) {}
void doSomething()
{
cout << "do something " << i << endl;
}
};
int main() {
A* a = new A();
function<void()> func = std::bind(&A::doSomething, a);
delete a;
a = nullptr;
func();
return 0;
}
并得到输出:
do something 0
谁能帮我解释一下为什么我们可以在删除对象后调用对象方法?