为什么不为C ++有一个DELETE宏的原因 [英] Reason why not to have a DELETE macro for C++
本文介绍了为什么不为C ++有一个DELETE宏的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有任何好的理由(除了宏是邪恶,也许)不要使用以下宏?
Are there any good reasons (except "macros are evil", maybe) NOT to use the following macros ?
#define DELETE( ptr ) \
if (ptr != NULL) \
{ \
delete ptr; \
ptr = NULL; \
}
#define DELETE_TABLE( ptr ) \
if (ptr != NULL) \
{ \
delete[] ptr; \
ptr = NULL; \
}
推荐答案
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.
可能有一些奇怪的方式,你可以打破#define系统,但个人(这可能会让我呻吟;)我不认为这是一个很大的问题。
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.
这篇关于为什么不为C ++有一个DELETE宏的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文