If we declare a variable as const its content must not change even through a non const reference. In the following code I don't understand why I'm getting a different behavior depending on the type (primitive vs object):
#include <iostream>
class MyClass
{
public:
int m_value ;
MyClass(int value): m_value(value) {} ;
}
int main()
{
const int x = 0 ;
const_cast<int &>(x)=20 ;
std::cout << x << std::endl ; //output 0
const MyClass a(0) ;
const_cast<MyClass&>(a).m_value= 20 ;
std::cout << a.m_value << std::endl ; //output 20
}