Hello.
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++
Result of printing of pi will be 4. I used Visual C++ 2010 under Windows XP.
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?
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++
Code:
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?
Comment