#include <iostream>
 
struct wrapper
{
  wrapper() : data(6) { }
  void modify(int value) const
  {
    // this->data = value;                    // compile error: this is a pointer to const
    const_cast<wrapper*>(this)->data = value; // OK as long as the type object isn't const
  }
  int data;
};
 
int main() 
{
        int i = 6;                        // i is not declared const
  const int& ci = i; 
  const_cast<int&>(ci) = 42;              // OK: modifies i
  std::cout << "i = " << i << std::endl;  // OK, will be 42

  const int j = 6;                        // j is declared const
        int* pj = const_cast<int*>(&j);
            *pj = 42;                     // undefined behavior!
  std::cout << "j = " << j << std::endl;  // can be both 6 or 42

  wrapper w;
  w.modify(42);
  std::cout << "w.data = " << w.data << std::endl;

  const wrapper cw;
  cw.modify(42);                                      // undefined behavior!
  std::cout << "cw.data = " << cw.data << std::endl;  // can be both 6 or 42

  void (wrapper::*mp)(int) const = &wrapper::modify; // pointer to member function
  const_cast<void(wrapper::*)(int)>(mp); // compiler error: const_cast does not work on function pointers
}