std::unique_ptr<Investment, decltype(delInvmt)> // return type makeInvestment(Ts&&... params) { // ptr to be returned std::unique_ptr<Investment, decltype(delInvmt)> pInv (nullptr, delInvmt); if ( /* a Stock object should be created */ ) { pInv.reset(new Stock(std::forward<Ts>(params)...)); } else if ( /* a Bond object should be created */ )
//an example of auto_ptr void f2(){ std::cout << "f2" << std::endl; try { //If you pass a pointer to auto_ptr, //auto_ptr frees it when the control flow leaves the scope. std::auto_ptr<Investment> pInv(createInvestment()); throw 1; } catch (...) { std::cout << "catch" << std::endl; } }
Investment* return_row_ptr() { std::auto_ptr<Investment> pInv(createInvestment()); return pInv.get(); }
std::auto_ptr<Investment> return_auto_ptr() { std::auto_ptr<Investment> pInv(createInvestment()); return pInv; }