Originally Posted by
Nefisto
Here we get the mem position of the const char, and made an implicity conversion for (char*) to remove const properties, after we use * to get the value that is storaged in this mem position
You used an explicit conversion by doing the type cast, not an implicit conversion (which by definition would not involve the type cast notation).
Originally Posted by
Nefisto
this another one will be usefull if u want to change the value of an const vector
Don't do that! If something started off being declared const, attempting to change its value results in undefined behaviour. In fact, ptr should not even be a pointer to non-const char, i.e., while this is correct, it is poor practice:
Code:
char* ptr = NULL;
ptr = (char*)alpha;
It would be better to declare ptr to be a pointer to const char instead, and hence write:
Code:
const char *ptr = alpha;
This way, one would not accidentally attempt to modify the array of const char through ptr because something like this:
would result in an error or at least a warning from your compiler.
Generally, if you are casting away const-ness, it is because you know for sure that the underlying object is non-const, so you are just trying to say, eliminate a harmless const to non-const implicit conversion warning because of some API that you are working with.