dimanche 12 mars 2017

Behavior of const reference after casting to non-const

I am somewhat confused about the following piece of code. How can b still reference a but have a different value?

#include <iostream>
using namespace std;
int main()
{
    const int a = 5;
    const int &b = a;
    ++(int&)b;
    cout << a << endl;//returns 5
    cout << b << endl;//returns 6
    cout << "mem a:" << &a << endl; //returns 0x61ff18
    cout << "mem b:" << &b << endl; //returns 0x61ff18
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire