1

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 

}
Soulimane Mammar
  • 1,658
  • 12
  • 20
  • You have a constant object, calling a function which is marked as `const`, and then cast away the constness to modify the object. Why? What is the reason you do something like that? Plain curiosity? Or is there some underlying problem that you want to solve? If there's an underlying problem, then please ask about that directly instead. – Some programmer dude Feb 07 '19 at 08:32
  • 4
    Because your code modifies a const object it has *undefined behaviour*. There are no rules in C++ for how it should behave. If you really want to know why there is a difference then you need to look at your compiler and how it works. – john Feb 07 '19 at 08:34
  • const MyClass a(0) ; <- your Object is const. But .m_value member is not const, so it is all correct. – Xplatforms Feb 07 '19 at 08:34
  • 2
    _Except that any class member declared mutable ([dcl.stc]) can be modified, any attempt to modify ([expr.ass], [expr.post.incr], [expr.pre.incr]) a const object ([basic.type.qualifier]) during its lifetime ([basic.life]) results in undefined behavior:_ http://eel.is/c++draft/dcl.type.cv#4.sentence-1. – Daniel Langr Feb 07 '19 at 08:35
  • So what I'm getting is just a behavior of my compiler and it could be different on a different compiler – Soulimane Mammar Feb 07 '19 at 08:53
  • @SoulimaneMammar Yes exactly, for instance on another compiler your code could crash. – john Feb 07 '19 at 09:30
  • [Related](https://stackoverflow.com/questions/53759384/automatic-type-deduction-with-const-cast-is-not-working/53759564#53759564) (or rather, yet another explanation of `const_cast`). – Zereges Feb 07 '19 at 09:52
  • It seems that the 0 of the first output is not due to the actual value of x (I figured that out by printing another reference to x and the output was 20) but it is just an optimization of the compiler ( replacing x by 0 wherever it appear ). morality never ever try to change a const – Soulimane Mammar Feb 08 '19 at 06:51

1 Answers1

-2

You can't cast away the const-ness of the const variable x, since the variable x is a const variable.

what const_cast actually do is cast away the const pointer or reference , since your member m_value is not a const member , you can modify its value using const_cast

read more on this link

 int x = 0 ;
 const int& p = x; 
 // p = 20; // error: p is const
 const_cast<int &>(p)=20 ;
 std::cout << p << std::endl ; //output 20

-------------------------------------------

class MyClass { public:
const int m_value = 0; };

int main(){
  const MyClass a;
  const_cast<MyClass&>(a).m_value= 20 ;// undefined behavior
}

May
  • 101
  • 7