Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto_ptr to normal pointer conversion

Are we able to convert a std::auto_ptr to a normal pointer??

    class Test
    {
     ......
    }

    Test* function()
    {
      std::auto_ptr<Test> test(new Test());

      return _____//TODO : need to convert this auto_ptr to Test*
    }

Is it possible to convert an auto_ptr pointer which is created locally to normal pointer.

like image 290
Aneesh Narayanan Avatar asked Mar 02 '26 01:03

Aneesh Narayanan


2 Answers

Use release()

Test* function()
{
  std::auto_ptr<Test> test(new Test());

  return test.release()
}
like image 98
BЈовић Avatar answered Mar 03 '26 14:03

BЈовић


Is it possible to convert an auto_ptr pointer which is created locally to normal pointer.

Yes:

return test.release();
like image 43
NPE Avatar answered Mar 03 '26 14:03

NPE