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 宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么没有 C++ 的 DELETE 宏


基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01