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

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

    Personally I prefer the following

    template< class T > void SafeDelete( T*& pVal )
    {
        delete pVal;
        pVal = NULL;
    }
    
    template< class T > void SafeDeleteArray( T*& pVal )
    {
        delete[] pVal;
        pVal = NULL;
    }
    

    They compile down to EXACTLY the same code in the end.

    There may be some odd way you can break the #define system but, personally (And this is probably going to get me groaned ;) I don't think its much of a problem.

提交回复
热议问题