I think I may have a gross misunderstanding of the concept discussed here. Does "const", by any chance, only prevent the data type from being changed? |
const doesn't prevent the data type from being changed. As the last post pointed out, const has nothing to do with type, const only makes the variable immutable i.e. the value doesn't (can't be) changed. Unless of course const_cast is used but let's just ignore that for now.
Also I don't understand the point you were trying to make with the function you posted, if you wanted to change the value of 'x', just put 'global x' in the function and the output will be the same as that of @
ne555' output.
In most languages where objects are pass by reference (java, python, c#, c/c++ if working with pointers, etc ), most people assume that assigning that reference (or pointer) a different value changes the value of the object. What you don't realize is that the reference (or pointer) is pass-by-value. So in your example, you haven't really proven anything if your goal was to show that changing the value of x in the function doesn't change its value outside the function, because this is expected behavior.
In the code I posted, you can replace that bool with something like string and with the correct value and the example still holds.
Just rereading your first post and wanted to point out that although the compiler does enforce not changing the value of a const variable, the idea of const is lost after the program has been compiled into machine language (I might be wrong with this). At the machine language level i.e. 1's and 0's, there is no such thing as const; a value either exists or it doesn't. I point this out to show that there is no optimization going on by using const, it is just a way for the compiler to enforce that a variable is not to be modified for the duration of the program, and if this happens to produce code where that value is not modified, so be it. But after the compiler has finished, all that exists is just a value and if you have used a game cheat engine before, you know these values can be changed.