NULL check before deleting an object with an overloaded delete

前端 未结 11 1809
野性不改
野性不改 2020-12-18 18:24

This came up as one of the code review comments.

Is it a good idea to check for NULL before calling delete for any object?

I do understand delete operator c

11条回答
  •  悲哀的现实
    2020-12-18 18:26

    delete (T*)0; is valid and does nothing, similarly free(NULL); is also valid and does nothing. If you overload the delete operator, your implementation should carry the same semantics. The standard says how the standard delete will work, but I don't think it says how an overloaded delete should behave. For the sake of consistency with the standard/default behaviour, it should allow (T*)0 as input.

提交回复
热议问题