*p
calls auto_ptr::operator*
, which dereferences the managed pointer.
*p.get
first calls method auto_ptr::get
, which returns the managed pointer, which is then dereferenced by operator *
.
These will provide exactly the same result once executed: the managed pointer is dereferenced, and there will be no additional checking when using get
.
Note that auto_ptr
is deprecated since C++11. It is dangerous because ownership of the pointer is transfered when copying:
std::auto_ptr<int> p(new int(42));
{
std::auto_ptr<int> copy_of_p(p); // ownership of *p is transfered here
} // copy_of_p is destroyed, and deletes its owned pointer
// p is now a dangling pointer
To avoid the problem, you had to "manage the managed pointers":
std::auto_ptr<int> p(new int(42));
{
std::auto_ptr<int> copy_of_p(p); // ownership of *p is transfered here
// ...
p = copy_of_p; // p gets back ownership
} // copy_of_p is destroyed, but doesn't delete pointer owned by p
// p is still valid
Use unique_ptr
or shared_ptr
instead.
std::auto_ptr
is deprecated and will be removed in C++17. Usestd::unique_ptr
instead. – Autogamyauto_ptr
makes your whole code insecure in my opinion. Don't use it. – Brinn