about the safety of auto_ptr<class T>

P

Panjandrum

i want to show:

1). auto_ptr is not so safe.
2). given 1) is true, then it seems no much reason to use auto_ptr,
because we can use normal pointer safely, with care.

in this respect, auto_ptr does not have much obvious advantage over
normal pointer.

It also has many disadvantages and usability traps. And, most of all,
auto_ptr is not a pointer. Just an object that implements an
operator*() function.
 
V

Victor Bazarov

Panjandrum said:
It also has many disadvantages and usability traps. And, most of all,
auto_ptr is not a pointer. Just an object that implements an
operator*() function.

.... and the operator->() function ... and very specific copying ...
 
R

red floyd

Victor said:
... and the operator->() function ... and very specific copying ...

I use auto_ptr quite often, and use it for for specific purposes.
If I could, I'd use a local object, but I'm creating from a factory for
polymorphic behaviour.

i.e.

class Base { ... };
Base* CreateMemberOfBaseFamily(const std:string&);

void f()
{
std::string s;


// read s from some input source


std::auto_ptr pBase(CreateMemberOfBaseFamily(s));
// do stuff with pBase here, that includes the
// possibility of exceptions!!!!

// I don't have to worry about deleting pBase here,
// and I don't have to worry about catching any
// and all exceptions, because pBase will be cleaned
// up automatically
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top