Search code examples
c++delete-operator

How does delete deal with pointer constness?


I was reading this question Deleting a const pointer and wanted to know more about delete behavior. Now, as per my understanding:

delete expression works in two steps:

  1. invoke destructor
  2. then releases the memory (often with a call to free()) by calling operator delete.

operator delete accepts a void*. As part of a test program I overloaded operator delete and found that operator delete doesn't accept const pointer.

Since operator delete does not accept const pointer and delete internally calls operator delete, how does Deleting a const pointer work ?

Does delete uses const_cast internally?


Solution

  • const_cast doesn't really do anything – it's a way to suppress compiler moaning about const-ness of the object. delete keyword is a compiler construct, the compiler knows what to do in this case and doesn't care about const-ness of the pointer.