Using auto_ptr :: C++
Hi.
Under what situation should one use auto_ptr instead of plain pointer? I am in a situation where I cannot decide on using auto_ptr or pointer. I like auto_ptr because I trust that if something goes wrong at run-time, auto_ptr will help eliminate possible memory leaks. When do you recommend using auto_ptr?
Thanks,
Kuphryn
P.S. auto_ptr declaration cannot be in a switch:
switch (variable)
{
case 1 : auto_ptr temp(new class); // illegal
...
}
declare a new epoch and it will let you instance it there.
don''t use auto_ptr if you''re going to copy it alot (as when using the STL containers)
And use CComPtr for COM interfaces, otherwise, use auto_ptr whenever you want.
don''t use auto_ptr if you''re going to copy it alot (as when using the STL containers)
And use CComPtr for COM interfaces, otherwise, use auto_ptr whenever you want.
Okay. Thanks.
I think I figure out the essense of auto_ptr. auto_ptr is a quick and easy exception handling for class objects.
Here is a performance question. I always make sure all classes I use are secure from memory leak. I implement exception handling when dealing with many standard library functions that could return errors, i.e. new. Anyways, is it better to use auto_ptr and let the class handles unexpected run-time errors, or is it better to make sure the class is completely error proof (as much as possible)?
Kuphryn
Edited by - kuphryn on December 28, 2001 12:16:27 AM
I think I figure out the essense of auto_ptr. auto_ptr is a quick and easy exception handling for class objects.
Here is a performance question. I always make sure all classes I use are secure from memory leak. I implement exception handling when dealing with many standard library functions that could return errors, i.e. new. Anyways, is it better to use auto_ptr and let the class handles unexpected run-time errors, or is it better to make sure the class is completely error proof (as much as possible)?
Kuphryn
Edited by - kuphryn on December 28, 2001 12:16:27 AM
here''s a good article on auto_ptr:
Using auto_ptr effectively
also, pay attention when using auto_ptr, because it can lead to some really weird behaviour, i recommend you take a look at the smart pointer classes at www.boost.org for alternatives
(btw: stl containers of auto_ptr''s should not compile)
Using auto_ptr effectively
also, pay attention when using auto_ptr, because it can lead to some really weird behaviour, i recommend you take a look at the smart pointer classes at www.boost.org for alternatives
(btw: stl containers of auto_ptr''s should not compile)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement