64,639
社区成员
发帖
与我相关
我的任务
分享
MyClass aaa;
aaa.Print();
aaa.~MyClass();
aaa.Print(); // 上面不是析构释放了吗,怎么这里还能正常调用呢?
#include <iostream>
using namespace std;
class A
{
public:
void fun()
{
cout << "This is in fun" << endl;
}
~A()
{
cout << "This is in ~dtor" << endl;
}
};
int main()
{
A a;
a.fun();
a.~A();
a.fun();
A* b = new A;
b->fun();
delete b; // 按道理都已经delete b了,但是下面的调用仍然可以成立
b->fun();
// 甚至这样都是可以的:
b = NULL;
b->fun();
return 0;
}