delete如何处理指针常量? [英] How does delete deal with pointer constness?
问题描述
我正在阅读此问题删除const指针,并想进一步了解删除
行为。现在,根据我的理解:
I was reading this question Deleting a const pointer and wanted to know more about delete
behavior. Now, as per my understanding:
删除表达式
可以通过两个步骤进行操作:
delete expression
works in two steps:
- 调用析构函数
- 然后释放内存(通常调用
free()
),方法是调用操作符删除。
- invoke destructor
- then releases the memory (often with a call to
free()
) by calling operator delete.
操作符删除
接受 void *
。作为测试程序的一部分,我重载了 operator delete
,发现 operator delete
不接受 const
指针。
operator delete
accepts a void*
. As part of a test program I overloaded operator delete
and found that operator delete
doesn't accept const
pointer.
由于运算符删除不接受const指针,并且在内部进行删除,因此调用了运算符delete,删除const指针是工作吗?
Since operator delete does not accept const pointer and delete internally calls operator delete, how does Deleting a const pointer work ?
是否删除
在内部使用const_cast吗?
Does delete
uses const_cast internally?
推荐答案
const_cast并没有真正做任何事情–这是一种抑制编译器抱怨对象常数的方法。 delete 关键字是一个编译器结构,编译器知道在这种情况下该怎么做,并且不关心指针的常数。
const_cast doesn't really do anything – it's a way to suppress compiler moaning about const-ness of the object. delete keyword is a compiler construct, the compiler knows what to do in this case and doesn't care about const-ness of the pointer.
这篇关于delete如何处理指针常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!