Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
I cannot see the need for auto_ptr?!
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Karl Heinz Buchegger, post: 1465630"] Says who? Sometimes you don't have a choice. You are concentrating too much on returning pointers. void bar( int Type ) { Obj* pObj; pObj = Factory.CreateObject( Type ); // do something // object no longer needed, delete it. delete pObj; } Now lets say, that the part // do something is rather complicated and there is a chance that things might go wrong. You insert some tests and do a return. But when doing so you must not forget do delete the object given to you from the factory. void bar( int Type ) { Obj* pObj; pObj = Factory.CreateObject( Type ); // do something ... if( SomeTest ) { delete pObj; return; } if( SomeOtherTest ) { ... while( ... ) { if( AThirdTest ) return; } } // object no longer needed, delete it. delete pObj; } So you see the big? Deep inside the while, there is an if which returns. It happened to me, that I forgot to delete pObj, because I am human :-) An auto_ptr saves me from all of this. I don't need to remember to delete pObj, it is done automatically for me. For the very same reason that you use vector instead of dynamically allocated arrays, you use an auto_ptr when you must use a pointer, to free yourself from human errors and to easen maintenance (who says that this test was there in the first place. It could have been added weeks later). [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
I cannot see the need for auto_ptr?!
Top