/////////////////////////////////////////////////// // MT4Q5.cpp - Assert // // // // Jim Fawcett, CSE687 - Midterm, Spring 2010 // /////////////////////////////////////////////////// #include #include // this class simply provides a name to be // announced when an assertion fails. class Test { public: virtual ~Test() {} std::string what() { std::string temp = typeid(*this).name(); temp += std::string(" failed"); return temp; } }; // Assertion class template class Assert { public: template static void that(A a) { if(!a) throw E(); } }; class Test_Alloc : public Test { }; class Widget { }; void main() { std::cout << "\n MT4Q5 - Assert throws Exception"; std::cout << "\n ================================="; Widget* pWidget = 0; // some code that tries to allocate a widget try { Assert::that(pWidget != 0); } catch(Test_Alloc& ta) { std::cout << "\n\n " << ta.what().c_str() << "\n\n"; } }