问题描述
是否有区别:
操作符delete(some_pointer);
和
delete some_pointer;
如果有什么区别,应该使用哪一个, ?具有讽刺意味的是, delete
操作符和 operator delete()
是不一样的。
delete some_pointer;
调用 some_pointer
的析构函数,然后调用 operator delete()
释放内存。
您通常不直接调用 operator delete()
,因为如果这样做,对象的析构函数将不会
只有当你需要关心操作符delete()$ c时,才会出现内存泄漏。 $ c>是当你想通过重写
operator new()
和操作符delete()
。
还要注意, delete
和 delete [ ]
是两个不同的东西。
Is there a difference between:
operator delete(some_pointer);
and
delete some_pointer;
and if so what is the difference and where one should use one and where the other version of this operator?Thanks.
Ironically, the delete
operator and operator delete()
are not the same thing.
delete some_pointer;
calls some_pointer
's destructor, and then calls operator delete()
to free the memory.
You do not normally call operator delete()
directly, because if you do, the object's destructor will not be called, and you are likely to end up with memory leaks.
The only time you have to care about operator delete()
is when you want to do your own memory management by overriding operator new()
and operator delete()
.
To top it off, you should also be aware that delete
and delete []
are two different things.
这篇关于哪个操作员删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!