struct Foo { }; struct Bar : public Foo { }; struct Schmoo { }; enum direction { NORTH, SOUTH, EAST, WEST }; int main() { //////////////////////////////////////////////////////////////////////////////////////////////////////// // the following statement should not compile if Bar inherited protected or private from Foo // Foo* f = new Bar; //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try to cast from base class to derived class Foo* f = new Bar; Bar* b1 = static_cast(f); Bar* b2 = reinterpret_cast(f); // This one does not work because there are no virtual functions in Foo // Bar* b3 = dynamic_cast(f); //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try to cast from unrelated class pointer Schmoo* s2 = reinterpret_cast(f); // these are not valid // Schmoo* s1 = static_cast(f); // Schmoo* s3 = dynamic_cast(f); //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try to cast away constness Bar const * b4 = b1; Bar* b9 = const_cast(b4); // these are not valid // Bar* b6 = static_cast(b4); // Bar* b7 = dynamic_cast(b4); // Bar* b8 = reinterpret_cast(b4); //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try to assign enum to int and int to enum direction d = NORTH; int i = 0; i = d; // this one requires a cast // d = i; //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try to cast an int to an enum d = static_cast(i); // these are not valid // d = reinterpret_cast(i); // d = dynamic_cast(i); // d = const_cast(i); //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try assign pointer to long long lng; lng = reinterpret_cast(f); // these are not valid // long lng = f; // long lng = static_cast(f); // long lng = dynamic_cast(f); // long lng = const_cast(f); //////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////// // Try assign long to pointer f = reinterpret_cast(lng); // these are not valid // f = lng; // f = static_cast(lng); // f = dynamic_cast(lng); // f = const_cast(lng); //////////////////////////////////////////////////////////////////////////////////////////////////////// return 0; }