A
Anshul Sawant
Have a look at the following code:
#include<iostream>
using namespace std;
int main()
{
const int a = 1;
const int* const aptr1 = &a;
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}
Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)
1
1
2
2
Is it the correct behaviour?
Is it due to constant folding?
If yes, how is constant folding possible when we are referencing the variable?
#include<iostream>
using namespace std;
int main()
{
const int a = 1;
const int* const aptr1 = &a;
int* const aptr2 = const_cast<int*>aptr1);
*aptr2 = 2;
cout<<a<<endl;
cout<<*(&a)<<endl;
cout<<*aptr1<<endl;
cout<<*aptr2<<endl;
}
Output is (for g++ (ver. 3.2) and visual C++ 6 compiler)
1
1
2
2
Is it the correct behaviour?
Is it due to constant folding?
If yes, how is constant folding possible when we are referencing the variable?