/* * policy-test.cc version 3 - Conversion, checking and storage policies implemented. */ #include #include "smart_pointer.h" #include "testutils.icc" using namespace std; #ifdef ALLOW_CONVERSION #ifdef NO_CHECK typedef smart_pointer Smart_Pointer; #elif REJECT_NULL typedef smart_pointer Smart_Pointer; #elif REJECT_NULL_DEREF typedef smart_pointer Smart_Pointer; #elif ASSERT_CHECK typedef smart_pointer Smart_Pointer; #elif ASSERT_CHECK_DEREF typedef smart_pointer Smart_Pointer; #endif #else #ifdef NO_CHECK typedef smart_pointer Smart_Pointer; #elif REJECT_NULL typedef smart_pointer Smart_Pointer; #elif REJECT_NULL_DEREF typedef smart_pointer Smart_Pointer; #elif ASSERT_CHECK typedef smart_pointer Smart_Pointer; #elif ASSERT_CHECK_DEREF typedef smart_pointer Smart_Pointer; #endif #endif int main() try { Smart_Pointer sp1; #ifndef NO_CHECK try { sp1->get_m(); } catch (const exception& e) { cout << e.what() << " for 'sp1->get_m()'" << endl; } #endif Smart_Pointer sp2{ new X{ 1 } }; // One X is created here. Smart_Pointer sp3{ sp2 }; // An X is copied here (we now have two X). cout << "sp2->get_m(): " << sp2->get_m() << endl; cout << "sp3->get_m(): " << sp3->get_m() << endl; sp1 = sp3; // An X is copied here (we now have three X). cout << "sp3->get_m(): " << sp3->get_m() << endl; cout << "sp1->get_m(): " << sp1->get_m() << endl; cout << "sp2->get_m(): " << sp2->get_m() << endl; #ifdef ALLOW_CONVERSION X* xp = sp1; cout << "xp->get_m(): " << xp->get_m() << endl; #endif return 0; // Three X are now to be destructed. } catch (const exception& e) { cout << e.what() << endl; } catch (...) { cout << "Caught an exception not of type 'exception'" << endl; }