delete如何处理指针常量? [英] How does delete deal with pointer constness?

查看:216
本文介绍了delete如何处理指针常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读此问题删除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:


  1. 调用析构函数

  2. 然后释放内存(通常调用 free()),方法是调用操作符删除。

  1. invoke destructor
  2. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆