Reason why not to have a DELETE macro for C++

前端 未结 12 1134
臣服心动
臣服心动 2020-11-29 07:01

Are there any good reasons (except \"macros are evil\", maybe) NOT to use the following macros ?

#define DELETE( ptr ) \\
i         


        
12条回答
  •  伪装坚强ぢ
    2020-11-29 07:26

    1. It doesn't give you much benefit. Deleting a null pointer is harmless, so the only benefit is setting the pointer to NULL after the delete. If a developer can remember to call your macro rather than delete, she can also remember to null out the pointer, so you're not really protecting yourself from a careless developer. The only benefits is that this happens in two lines rather than one.
    2. It's potentially confusing. delete is a standard part of the language. Your macro or templated function is not. So a new developer will need to look up that macro definition to understand what your code is doing.

    In my judgement, the benefit does not outweigh the cost.

提交回复
热议问题