Jump to content
  • Advertisement
Sign in to follow this  
Trillian

Is this a "moral" use of const_cast?

This topic is 4053 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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!