// const-cast.cc #include using namespace std; class MyClass { public: void Print() { // should be declared const but isn't cout << "Object prints!" << endl; } }; // const below promises PrintData() won't change X, but // we didn't declare MyClass::Print() as const, so X.Print() looks // like trouble to the compiler void PrintData(const MyClass& X) { // X.Print(); // compiler gives error MyClass& Y = const_cast (X); // workaround Y.Print(); } int main() { MyClass X; PrintData(X); }