C++ -- How can we call "delete this; " in a const-member function?

Posted by q0987 on Stack Overflow See other posts from Stack Overflow or by q0987
Published on 2011-02-28T23:08:21Z Indexed on 2011/02/28 23:24 UTC
Read the original article Hit count: 221

Filed under:
|
|

Hello all,

I saw the code snippet as follows:

class UPNumber {
public:
  UPNumber();
  UPNumber(int initValue);
  ...

  // pseudo-destructor (a const member function, because
  // even const objects may be destroyed)
  void destroy() const { delete this; } // why this line is correct???

  ...

private:
  ~UPNumber();
};

First, I am sure that above class definition is correct. Here is my question, why we can define the function 'destroy' as above? The reason being asking is that why we can modify 'this' in a const-member function?

Thank you

© Stack Overflow or respective owner

Related posts about c++

Related posts about delete