#define BOOST_TEST_MODULE Serialization #include #include class Base1 { int m_i1; protected: Base1() : m_i1(1) { } ~Base1() { } }; class Base2 { double m_i2; protected: Base2() : m_i2(2) { } ~Base2() { } }; class Derived1 : public Base1, public Base2 { double m_d1; public: Derived1( ) : m_d1(1.0) {} }; class Derived2 : public Base2, public Base1 { double m_d2; public: Derived2( ) : m_d2(1.0) {} }; class Derived : public Base1 { double m_d; public: Derived( ) : m_d(0.0) {} }; BOOST_AUTO_TEST_SUITE(Serialization) BOOST_AUTO_TEST_CASE(pointerTest) { // Declare the derived objects Derived d; Derived1 d1; Derived2 d2; // Assign the derived objects to void pointers void *pVoidD = &d, *pVoidD1 = &d1, *pVoidD2 = &d2; // Cast the void pointers back to their derived types, // and assign them to Base1 pointers Base1 *pB = static_cast(pVoidD), *pB1 = static_cast(pVoidD1), *pB2 = static_cast(pVoidD2); // Cast the Base1 pointers back to the respective derived type, // and assign them to the derived pointers Derived *pD = static_cast(pB); Derived1 *pD1 = static_cast(pB1); Derived2 *pD2 = static_cast(pB2); // Cast the processed derived pointers to integers, for easier comparison unsigned long long iD = reinterpret_cast(pD), iD1 = reinterpret_cast(pD1), iD2 = reinterpret_cast(pD2), iB = reinterpret_cast(pB), iB1 = reinterpret_cast(pB1), iB2 = reinterpret_cast(pB2); BOOST_REQUIRE(iD == iB); // We expect that Derived is equal to Base1 BOOST_REQUIRE(iD1 == iB1); // We expect that Derived1 is equal to Base1 BOOST_REQUIRE(iD2 != iB2); // We expect that Derived2 is NOT equal to Base1, // since it inherits from Base2, Base1, respectively, it will be equal to Base2 // This means that if using static_cast one can use multiple inheritance } BOOST_AUTO_TEST_SUITE_END()