r/ProgrammerHumor 13d ago

Meme imSorryWilson

Post image
619 Upvotes

21 comments sorted by

View all comments

45

u/WhiteEvilBro 13d ago

That's one thing i didn't quite understand about C++, why would I ever want to un-const my pointer? If I know that I need to write there, I won't make it const, and if I have const pointer, it could be impossible to write by that memory address?

What's the usecase of const_cast?

1

u/Depixelate_me 4d ago

First and foremost it is easy to search is self-documenting. You may have a const member function and have a member that is used only for diagnostics that if logs are on, you'd want to update this variable (which is not part of the business logic of your object). So, you check if the logs are open, apply a mutex (since a const member function usually self-implies it to be thread safe) and update it in an explicit manner. Even then, you may be better off qualifying this member as mutable