c++ const_cast

Aug 27, 2016 at 4:02pm
Why the statement below print one address and two values:

#include <iostream>
using namespace std;

int main() {
const int i = 2147483646;
int *j = const_cast<int*>(&i); // Preferred
*j = 2147483647;
cout << i <<endl<<*j <<endl<<endl;
cout <<&i <<endl<<j <<endl;
}

Thank's whom will answer.
All the best
Last edited on Aug 27, 2016 at 4:02pm
Aug 27, 2016 at 4:41pm
Undefined behaviour.

Except that any class member declared mutable can be modified,
any attempt to modify a const object during its lifetime results in undefined behaviour. - IS


undefined behaviour: Renders the entire program meaningless http://en.cppreference.com/w/cpp/language/ub
Aug 29, 2016 at 8:55am
Thank you very much!
Topic archived. No new replies allowed.