• Advertisement
Sign in to follow this  

Is this a "moral" use of const_cast?

This topic is 3879 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I have a base class in my code called SharedObject which adds reference counting capabilities to derived objects. The function used to increment and decrement the reference count are respectively "grab" and "drop". I figured that you might want to use those two functions even if you only have a constant pointer (even if you can only read from the pointer, you still want to ensure that the object being pointed to continues to exist as long as the pointer is used). So I thought I could make "grab" and "drop" const, make the internal "m_refCount" mutable and use a const_cast in "drop" in order to be able to "delete this". Have someone ever done something similar, then realised that it wasn't a good idea and that const pointers shouldn't be able to call the likes of "grab" and "drop"? I've seen many people here who seemed to think that there never was a good reason to use const_cast so I want to have some of your opinions.

Share this post


Link to post
Share on other sites
Advertisement
You can delete a const pointer.

Share this post


Link to post
Share on other sites
It's legal to call delete on a pointer to const, so no const_cast is necessary.

Share this post


Link to post
Share on other sites
Oh I didn't knew this! Thanks!

I still find that a little weird, a function to which I pass a const pointer could actually delete the pointer? I hope that never happens to me!

Share this post


Link to post
Share on other sites
Well, the obvious solution is to avoid functions that accept pointers to const. Most such functions could be written with const references instead and be perfectly happy. <insert standard glowing recommendation for smart pointers here>

As an aside, there is exactly one const_cast in my current project. Whoever wrote PyImport_ImportModuleEx() for the Python C API decided to use a char * type for a null terminated string argument. Bastard.

Share this post


Link to post
Share on other sites
Quote:
Original post by Trillian
Oh I didn't knew this! Thanks!

I still find that a little weird, a function to which I pass a const pointer could actually delete the pointer? I hope that never happens to me!


Why would you expect otherwise? 'const' protects the *value* of the *variable itself*, while 'delete' merely deallocates memory (not necessarily even changing its contents!) *at the address* stored by the pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by Trillian
I still find that a little weird, a function to which I pass a const pointer could actually delete the pointer? I hope that never happens to me!

Why would you expect otherwise? 'const' protects the *value* of the *variable itself*, while 'delete' merely deallocates memory (not necessarily even changing its contents!) *at the address* stored by the pointer.

In addition to deallocation, delete also calls the destructor. And, "not necessarily even changing its contents" is an odd claim.

Anyway, const variables get destroyed when they go out of scope, so delete should work on const variables, too.

Share this post


Link to post
Share on other sites
I find it strange you can delete const pointers too. It calls the destructor, which is non-const. The destructor can (and usually does) modify member data and can call non-const member functions. So it's basically just like doing a const_cast<>.

Share this post


Link to post
Share on other sites
I presume you're all talking about deleting via a pointer to const? I.e
const int *p = new int;
delete p;
What about deleting a const pointer to something. I.e
int *const p = new int;
delete p;
Is that also allowed? My initial though is that it isn't, because delete is allowed to NULL the pointer passed to it. I've read Stroustrup hoped compiler vendors would do that, although most don't.

Share this post


Link to post
Share on other sites
Keep in mind that when you read something from Stroustrup that you could be potentially reading something from any point along the development of C++. IIRC, having a compiler automatically null out a pointer on delete was ruled out for two reasons: 1) delete can be called on an rvalue, in which case nulling it is ill-formed. 2) You can have multiple pointers to the same object and delete would only be able to affect one of those pointers. Therefore having the compiler automatically null out the variable would give you a false sense of security.

Share this post


Link to post
Share on other sites
I found This webpage very useful in helping myself understand what exactly all this constant stuff meant.

Take a look... it enlightened me, and I have been using constants for a long time now.

~Chris

Share this post


Link to post
Share on other sites
The only time you should really use const_cast is when you are using a legacy C library, which has no concept of const, so you need to hide the "constness" of a pointer you are passing to it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Malone
The only time you should really use const_cast is when you are using a legacy C library, which has no concept of const, so you need to hide the "constness" of a pointer you are passing to it.
You know as soon as you put the word 'only' in there someone is going to come up with another reason.
I know of a few places in ATL & COM code that you need to use const. IIRC, IDL files don't let you speicify the constness of parameters.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
As an aside, there is exactly one const_cast in my current project. Whoever wrote PyImport_ImportModuleEx() for the Python C API decided to use a char * type for a null terminated string argument. Bastard.


<ignorant>
C API can only use char* for string and null-terminated strings... right? So in short, why is he a bastard?
</ignorant>

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Quote:
Original post by SiCrane
As an aside, there is exactly one const_cast in my current project. Whoever wrote PyImport_ImportModuleEx() for the Python C API decided to use a char * type for a null terminated string argument. Bastard.


<ignorant>
C API can only use char* for string and null-terminated strings... right? So in short, why is he a bastard?
</ignorant>


Because he didn't make it const char*?

As far as I know - C supports keyword const...

Share this post


Link to post
Share on other sites
C has const. It has for a long time. Maybe not quite as long as I've been alive, but close. And there are other functions in the Python C API that accept null terminated strings use const char *. Like PyImport_ImportModule(). You know, the function right next to PyImport_ImportModuleEx().

Share this post


Link to post
Share on other sites
A very slight divergance from the topic; why do you need to cast const-ness onto a type? I understand away from, but it seems harmless to automatically add a const to something that's, say, being passed into a function. I would think that allowing it to be done automatically would encourage using const, since adding it in later would not affect other code nearly as much, though it would be inconsistent.

Share this post


Link to post
Share on other sites
There's an implicit conversion from a non-const pointer, reference, etc. To a const pointer, reference, etc. So there's no need for a const_cast in that direction. Or maybe I didn't understand your question.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement