// From: http://en.cppreference.com/w/cpp/language/const_cast #include class MyType { public: MyType() {i=3;} void m1(int v) const { // this->i = v; // compile error: this is a pointer to const const_cast(this)->i = v; // OK as long as the MyType object isn't const, // in which case what happens is undefined } int get() { return i; } private: int i; }; int main() { int i = 3; // i is not declared const const int& cref_i = i; const_cast(cref_i) = 4; // OK: modifies i std::cout << "i = " << i << std::endl; MyType t; // note, if this is const MyType t;, then t.m1(4); is UB t.m1(5); std::cout << "MyType::i = " << t.get() << std::endl; const int j = 3; // j is declared const int* pj = const_cast(&j); *pj = 4; // undefined behavior! void (MyType::*mfp)(int) const = &MyType::m1; // pointer to member function // const_cast(mfp); // compiler error: const_cast does not // work on function pointers }