const qualifier is usually posed as better alternative to #define (because it allows to check types). The simple program shows how const variable can be changed in C++
Expand|Select|Wrap|Line Numbers
- const double pi = 3.1415926;
- double& pi_copy = (double&)pi;
- pi_copy = 4.0;
- cout << pi << endl;
As far as I understand, constant variables, defined with const qualifier, supposedly, cannot be changed in any way inside the program (otherwise the whole idea of "const" does not have any sense). Now, using an alias and simple casting we could change a const variable.
Question:
Is this behavior allowed by C++ standard or this is a compiler specific bug?