Пример #1
0
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 */ )
Пример #2
0
//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; 
  }
}
Пример #3
0
Investment* return_row_ptr()
{
  std::auto_ptr<Investment> pInv(createInvestment());
  return pInv.get();
}
Пример #4
0
std::auto_ptr<Investment> return_auto_ptr()
{
  std::auto_ptr<Investment> pInv(createInvestment());
  return pInv;
}