本文介紹了為什么沒有 C++ 的 DELETE 宏的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
是否有任何好的理由(可能除了宏是邪惡的")不要使用以下宏?
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 宏的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!