Deleting a pointer to const (T const*)
Asked Answered
S

5

105

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:

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?

Sheer answered 16/4, 2009 at 8:18 Comment(0)
B
129

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.

Bedesman answered 16/4, 2009 at 8:21 Comment(2)
+1 for latest your edit. I think this is true reason. Automatic destructor call for const object - the almost same as delete f; where f - pointer on const.Pool
const Foo * f or Foo const * fis not a const pointer to Foo. It is poiner to const Foo. Foo * const f is a const pointer to Foo.Spielman
D
58

Put it this way - if it weren't allowed there would be no way to delete const objects without using 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.

Denysedenzil answered 16/4, 2009 at 8:22 Comment(3)
Destructors can mutate objects in rather violent ways, so this must be some strange usage of the word 'immutable' that I wasn't previously aware of...Bowlds
@Bowlds no, destructors take you from a state where there is an object, to one where there isn't. C++ does not define any method to observe a "mutation" post destructionPhotoactinic
@ Caleth: the standard may not allow you to look at the object after its destructor has run to completion, but you are certainly allowed to look at side effects caused by the destruction. Hence circumstances can easily be arranged to make the mutation of the 'immutable' object observable. In the US, murder is difficult to prosecute when there is no body, but it is still murder (and there may be other evidence sufficient for a conviction). Same difference.Bowlds
W
5

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.

Willingham answered 16/4, 2009 at 8:30 Comment(0)
P
5

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).

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.)

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.

The usual solution to this is to use almost any other language instead.

Pyroelectricity answered 16/4, 2009 at 13:47 Comment(2)
If you can't destroy things pointed to by pointers to const, how do you deal with a std::unique_ptr<const T> ending it's life?Photoactinic
@Photoactinic then there would be no solution to that in C++. That is just one example of the general problem: in C++ the const modifier means “You can’t mutate the target, except in one sense where you can completely wreck it and render all other references to it invalid and sources of undefined behaviour”. This is why I think this kind of question should act as a prompt to consider other languages. It has UB holes in it that cannot be solved without taking a different basic approach.Pyroelectricity
H
5

I am not allowed to call any non-const member functions using a const pointer.

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.

Honoria answered 13/10, 2011 at 18:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.