Hello I'm new to c++ I've read that if I've a const variable I can remove its constness and update the variable as I please but in general it defeata the purpose of const . Can someone explain why one would ever make something const to remove its constness later?
Asked
Active
Viewed 45 times
1
-
You should remove the `const`-ness when you KNOW the variable will NOT be modified. Usually for C APIs that don't have a notion of `const`-ness, even though they don't modify the object (which is often an array of `char`, but could be a `struct`). – Eljay Mar 23 '21 at 19:01
-
when you hold a constant reference, it means that you cannot modify the object through that reference, it does not imply that the object is const – 463035818_is_not_an_ai Mar 23 '21 at 19:03
1 Answers
1
Mostly you should not do it, but some time you are stuck with a third party library which expects a char* ,but in your implementation you have the field marked as const , then you need to temporarily cast away the constness then make the function call.

Dharman
- 30,962
- 25
- 85
- 135

saumitra mallick
- 395
- 2
- 11