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

前端 未结 12 1084
臣服心动
臣服心动 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:17

    1. macros are evil :p Seriously, consider using inlined template functions instead
    2. setting a pointer to NULL after deallocation tends to mask errors
    3. encourages if (ptr != NULL) checks as a flow control mechanism. Personally, I consider this a code smell along the lines of void foo(int arg) being replaced with void foo(int arg, bool doAdvancedThings=false)
    4. encourages usage of raw pointers to memory that needs to be deleted - shared_ptr and its relatives should always be used for ownership, raw pointers can be used for other access
    5. encourages looking at a pointer variable after deallocation, even worse using if (ptr != NULL) instead of if (ptr)... comparing pointers is another code smell

提交回复
热议问题