Is it possible to prevent `delete` on a `const *`?
-
@JoeCFD said in Is it possible to prevent `delete` on a `const *`?:
- can call only const funcs
delete
does not only delete the contents something points to. It also calls the destructor of the underlying object. So, there is an inconsistency that I can call the destructor on a const object (which I never noticed in my long C++ career).Best advice is to not use plain owning pointers in C++. But, that would still leave you with the convention to use raw pointers as non-owning pointers with the technical possibility that someone calls
delete
on them. Making the delete operator private works to suppress this, but is also really intrusive to be of general use.@SimonSchroeder said in Is it possible to prevent `delete` on a `const *`?:
It also calls the destructor of the underlying object. So, there is an inconsistency that I can call the destructor on a const object (which I never noticed in my long C++ career).
Exactly! I am "surprised" that you have never "noticed" this, as I most certainly have, and is precisely why I am so shocked it is allowed! :) I am finding this whole "you cannot change the object via
const *
but feel free to completely clobber it by deleting" very odd! -
J JonB has marked this topic as solved on
-
@SimonSchroeder said in Is it possible to prevent `delete` on a `const *`?:
It also calls the destructor of the underlying object. So, there is an inconsistency that I can call the destructor on a const object (which I never noticed in my long C++ career).
Exactly! I am "surprised" that you have never "noticed" this, as I most certainly have, and is precisely why I am so shocked it is allowed! :) I am finding this whole "you cannot change the object via
const *
but feel free to completely clobber it by deleting" very odd!@JonB said in Is it possible to prevent `delete` on a `const *`?:
I am finding this whole "you cannot change the object via const * but feel free to completely clobber it by deleting" very odd!
Else, you would not be able to free the memory
-
@SimonSchroeder said in Is it possible to prevent `delete` on a `const *`?:
It also calls the destructor of the underlying object. So, there is an inconsistency that I can call the destructor on a const object (which I never noticed in my long C++ career).
Exactly! I am "surprised" that you have never "noticed" this, as I most certainly have, and is precisely why I am so shocked it is allowed! :) I am finding this whole "you cannot change the object via
const *
but feel free to completely clobber it by deleting" very odd!@JonB said in Is it possible to prevent `delete` on a `const *`?:
I am finding this whole "you cannot change the object via const * but feel free to completely clobber it by deleting" very odd!
freeing an objects memory is very much different from changing its internal state. const only prohibits the later
-
@JonB said in Is it possible to prevent `delete` on a `const *`?:
I am finding this whole "you cannot change the object via const * but feel free to completely clobber it by deleting" very odd!
freeing an objects memory is very much different from changing its internal state. const only prohibits the later
@J-Hilk said in Is it possible to prevent `delete` on a `const *`?:
freeing an objects memory is very much different from changing its internal state. const only prohibits the later
Well it may be "different" but it is equally "destructive". And ends up "changing its internal state" as a consequence. Hence the discussion. I now get that "const only prohibits the later", and that's life, but I still find it "odd".
-
I was just thinking about this: Is it possible to overload the delete operator with a const and non-const version?
-
I was just thinking about this: Is it possible to overload the delete operator with a const and non-const version?
@SimonSchroeder
So for my case that would do what, presumably runtime error? I was looking for a compile-time error on attempting todelete
aconst
pointer (like I would get on attempting to write to a member/call a non-const
member method). -
@SimonSchroeder
So for my case that would do what, presumably runtime error? I was looking for a compile-time error on attempting todelete
aconst
pointer (like I would get on attempting to write to a member/call a non-const
member method). -
@SimonSchroeder
So for my case that would do what, presumably runtime error? I was looking for a compile-time error on attempting todelete
aconst
pointer (like I would get on attempting to write to a member/call a non-const
member method).@JonB said in Is it possible to prevent `delete` on a `const *`?:
So for my case that would do what, presumably runtime error?
If you can distinguish that, you could make the const-version private and the non-const public. So, you can still normally delete objects when you are allowed to (with a pointer to non-const). But I'm not sure if this distinction is possible.
-
@JonB said in Is it possible to prevent `delete` on a `const *`?:
So for my case that would do what, presumably runtime error?
If you can distinguish that, you could make the const-version private and the non-const public. So, you can still normally delete objects when you are allowed to (with a pointer to non-const). But I'm not sure if this distinction is possible.
@SimonSchroeder said:
But I'm not sure if this distinction is possible
It's not. The delete operator can't be cv qualified.
Here's a fun quirk:
struct Foo { void itIsFine() const { delete this; } ~Foo() { bar = 42; } int bar = 0; }; const Foo* foo = new Foo(); foo->itIsFine();
so not only can you delete an object through a pointer to const, but a const function can mutate the object without
mutable
orconst_cast
by deleting the object it is being called on;)