Im starting to learn c++ and was under the impression that by putting const is means that the value wont change but i wrote the following code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
and it prints out 2 while i thought it would have given me an error for changing a const int value. I am using MSVS as my compiler
EDIT: I get a compiler warning saying C4530: C++ exception handler used, but unwind semantics are not enabled, specify /EHsc
It works now and gives me the correct error but does anyone know what this means
constis a plea to your compiler not to let you (nor anyone maintaining the code) assign to the variable. Technically the value represented by this identifier might be altered by subtle memory corruption (design errors in your system) or malfunction (hardware, etc). As written, your example zooms in on a much simpler case that indeed should not be allowed by valid compilers. Is it possible that you are abridging the real complexity of the source you've provided to the compiler?