I have a function that remove the const attribute of the int*
and change the value of the variable it point to, but it doesn't work since I pass a variable and reference it in the formal reference?
This is my code:
#include <bits/stdc++.h>
#include <iostream>
#include <map>
using namespace std;
typedef unsigned char UINT8;
int ll(const int &r)
{
*(const_cast<int *>(&r)) = 5;
// cout<<const_cast<int*> (&r)<<endl;
//*(&r)=5;
cout << r << endl;
}
int main()
{
const int a = 1;
ll(a);
cout << a << endl;
}
I expected the value shown in the function to be the same as the one in main()
, but it's different.