// Example code, illustrating the hazards of routine type-casting in C++. #include class Trouble { }; class Party { public: int const numLegs; Party() : numLegs(4) { } // C++ syntax for init'ing const numLegs as 4. }; int main() { Party *invitation = (Party*) (new Trouble()); // Every Party->numLegs is 4, and // C++'s type system states that invitation is a Party*, // so surely: // cout << "My dog has " << invitation->numLegs << " legs and a tail!" << endl; return 0; } // =============================================================== // Output: // // My dog has 246744 legs and a tail! // // Your mileage may vary, from one compiler/system to another. // The root of the problem is the blatantly illegal type cast, // which C++'s type system blithely allows, w/o so much as a peep: // // class Trouble { /* ...class details... */ }; // class Party { /* ...class details... */ }; // // Party *invitation = (Party*) (new Trouble()); // // C++ thinks invitation points to a Party, but really it is a Trouble. // // (Imagine trying to debug this, and discovering that changing // the order in which classes are declared -- which should change // nothing, according to every C++ manual -- can change the output!)