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++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01