After using 'delete this' in a member function I am able to access other member functions. Why?

前端 未结 2 1795
花落未央
花落未央 2020-12-12 00:02

I just wrote a sample program to see the behaviour of delete this

class A
 {
   ~A() {cout << \"In destructor \\n \";}
 public: 
    i         


        
2条回答
  •  感动是毒
    2020-12-12 00:59

    1. What you see is undefined behavior: it might work, or it may crash.
    2. The pointer is pointing to a deleted instance - it is a dangling pointer.
    3. This is an undefined behavior as well: you may see a zero or a garbage value.

    Eric Lippert provided a very nice "book in a table drawer of a hotel room" analogy in his answer to a question about pointers to local variables after the function has returned, it is equally applicable here.

提交回复
热议问题