C++ 删除指向 const (T const*) 的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/755196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting a pointer to const (T const*)
提问by Naveen
I have a basic question regarding the const pointers. I am not allowed to call any non-const member functions using a const pointer. However, I am allowed to do this on a const pointer:
我有一个关于 const 指针的基本问题。我不允许使用 const 指针调用任何非常量成员函数。但是,我可以在 const 指针上执行此操作:
delete p;
This will call the destructor of the class which in essence is a non-const 'method'. Why is this allowed? Is it just to support this:
这将调用类的析构函数,它本质上是一个非常量的“方法”。为什么这是允许的?是否只是为了支持这一点:
delete this;
Or is there some other reason?
还是有其他原因?
回答by
It's to support:
是为了支持:
// dynamically create object that cannot be changed
const Foo * f = new Foo;
// use const member functions here
// delete it
delete f;
But note that the problem is not limited to dynamically created objects:
但请注意,问题不仅限于动态创建的对象:
{
const Foo f;
// use it
} // destructor called here
If destructors could not be called on const objects we could not use const objects at all.
如果不能在 const 对象上调用析构函数,我们就根本不能使用 const 对象。
回答by PaulJWilliams
Put it this way - if it weren'tallowed there would be no way to delete const objects without using const_cast.
这么说吧-如果它未被允许就没有办法删除const对象不使用的const_cast。
Semantically, const is an indication that an object should be immutable. That does not imply, however, that the object should not be deleted.
从语义上讲,const 表示对象应该是不可变的。但是,这并不意味着不应删除该对象。
回答by Indy9000
Constructors and Destructors should not be viewed as 'methods'. They are special constructs to initialise and tear down an object of a class.
构造函数和析构函数不应被视为“方法”。它们是用于初始化和拆除类对象的特殊构造。
'const pointer' is to indicate that the state of the object would not be changed when operations are performed on it while it is alive.
'const 指针'是为了指示对象在它活着的时候对其执行操作时不会改变它的状态。
回答by Daniel Earwicker
Another way to look at it: the precise meaning of a const pointer is that you will not be able to make changes to the pointed-to object that would be visible via that or any other pointer or reference to the same object. But when an object destructs, all other pointers to the address previously occupied by the now-deleted object are no longer pointers to that object. They store the same address, but that address is no longer the address of any object (in fact it may soon be reused as the address of a different object).
另一种看待它的方式:const 指针的确切含义是,您将无法对通过该对象或任何其他指针或对同一对象的引用可见的指向对象进行更改。但是当一个对象析构时,所有其他指向之前被现在删除的对象占用的地址的指针不再是指向那个对象的指针。它们存储相同的地址,但该地址不再是任何对象的地址(实际上它可能很快会被重用为不同对象的地址)。
This distinction would be more obvious if pointers in C++ behaved like weak references, i.e. as soon as the object is destroyed, all extant pointers to it would immediately be set to 0
. (That's the kind of thing considered to be too costly at runtime to impose on all C++ programs, and in fact it is impossible to make it entirely reliable.)
如果 C++ 中的指针表现得像弱引用,则这种区别将更加明显,即一旦对象被销毁,所有现存的指向它的指针将立即设置为0
。(这种事情被认为在运行时成本太高,无法强加给所有 C++ 程序,实际上不可能让它完全可靠。)
UPDATE: Reading this back nine years later, it's lawyer-ish. I now find your original reaction understandable. To disallow mutation but allow destruction is clearly problematic. The implied contract of const pointers/references is that their existence will act as a block on destruction of the target object, a.k.a. automatic garbage collection.
更新:九年后再读一遍,这是律师式的。我现在发现你最初的反应是可以理解的。不允许突变但允许破坏显然是有问题的。const 指针/引用的隐含契约是它们的存在将作为目标对象销毁的块,也就是自动垃圾收集。
The usual solution to this is to use almost any other language instead.
通常的解决方案是使用几乎任何其他语言。
回答by Oktalist
I am not allowed to call any non-const member functions using a const pointer.
我不允许使用 const 指针调用任何非常量成员函数。
Yes you are.
是的,你是。
class Foo
{
public:
void aNonConstMemberFunction();
};
Foo* const aConstPointer = new Foo;
aConstPointer->aNonConstMemberFunction(); // legal
const Foo* aPointerToConst = new Foo;
aPointerToConst->aNonConstMemberFunction(); // illegal
You have confused a const pointer to a non-const object, with a non-const pointer to a const object.
您混淆了指向非常量对象的常量指针和指向常量对象的非常量指针。
Having said that,
话说回来,
delete aConstPointer; // legal
delete aPointerToConst; // legal
it's legal to delete either, for the reasons already stated by the other answers here.
由于其他答案已经说明的原因,删除任何一个都是合法的。