Why does C++ allow private members to be modified using this approach?
After seeing this question a few minutes ago, I wondered why the language designers allow it as it allows indirect modification of private data. As an example class TestClass { private: int cc; public: TestClass(int i) : cc(i) {}; }; TestClass cc(5); int* pp = (int*)&cc; *pp = 70; // private member has been modified I tested the above code and indeed the private data has been modified. Is there any explanation of why this is allowed to happen or this just an oversight in the language? It seems to directly undermine the use of private data members. Because, as Bjarne puts it, C++ is designed to