我只写了一个示例程序看到的删除行为
class A
{
~A() {cout << "In destructor \n ";}
public:
int a;
A() {cout << "In constructor \n ";}
void fun()
{
cout << "In fun \n";
delete this;
cout << this->a << "\n"; // output is 0
this->fun_2(); // how m able to call fun_2, if delete this is called first ??
}
void fun_2()
{
cout << "In fun_2 \n";
}
main()
{
A *ptr = new A;
ptr->a = 100;
ptr->fun(); //delete this will be executed here
ptr->fun_2(); //how m able to execute fun_2 when this pointer is deleted ??
cout<< ptr->a << "\n"; //prints 0
return 0;
}
> Output
In constructor
In fun
In destructor
0
In fun 2
In fun 2
0
问题
- 在执行删除乐趣之后 (),怎么我可以用这个指针访问func_2()中的乐趣()?
- 现在,在主如何,我能做到obj-> fun_2因为这个指针被删除?
- 如果我能够查杀此对象后访问成员函数,那么为什么数据成员为零正在添加“0”?
使用Linux Ubuntu和克间++编译器