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++ STL设置差异 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
