Why delete can perform on pointers to const while free cannot?

前端 未结 4 901
鱼传尺愫
鱼传尺愫 2021-02-14 06:22

I\'ve just noticed that the pointers passed to delete can be const qualified while those passed to free cannot. That is really a surprise

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 06:46

    One difference is that free is a library function and you must match the parameter type. When free was added to the C language there was no const keyword, so that couldn't even be considered.

    In C++ delete p is a statement handled by the compiler. It can do whatever the language designer wants it to, including not consider if the object pointed to is const or not.

    Philosophically speaking, the delete statement doesn't modify the object, it just makes it go away.

提交回复
热议问题