7

Why would I use get() with *, instead of just calling *?

Consider the following code:

auto_ptr<int> p (new int);
*p = 100;
cout << "p points to " << *p << '\n';           //100

auto_ptr<int> p (new int);
*p.get() = 100;
cout << "p points to " << *p.get() << '\n'; //100

Result is exactly the same. Is get() more secure?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
user2856064
  • 541
  • 1
  • 8
  • 25

4 Answers4

8

Practically no difference.

In case of *p, the overloaded operator* (defined by auto_ptr) is invoked which returns the reference to the underlying object (after dereferencing it — which is done by the member function). In the latter case, however, p.get() returns the underlying pointer which you dereference yourself.

I hope that answers your question. Now I'd advise you to avoid using std::auto_ptr, as it is badly designed — it has even been deprecated, in preference to other smart pointers such as std::unique_ptr and std::shared_ptr (along with std::weak_ptr).

Nawaz
  • 353,942
  • 115
  • 666
  • 851
6

*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.

LW001
  • 2,452
  • 6
  • 27
  • 36
rocambille
  • 15,398
  • 12
  • 50
  • 68
4

There is no effective difference. operator* is defined to return *get() (cppreference).

You should consider using unique_ptr in auto_ptr's stead. The latter has been removed from the current C++ standard and has very non-intuitive yank-on-copy behaviour.

krzaq
  • 16,240
  • 4
  • 46
  • 61
0

Don't use get() on a smart pointer (regardless of auto_, unique_, shared_, or otherwise). This returns a naked pointer that isn't under the control of an RAII class. This opens up various possibilities of giving the pointer to another RAII class (causing double-deletion later), or the caller doing something silly like deleting the pointer (again causing double-deletion). Just dereference the smart pointer.

PS: For experts only: Yes, there are other legitimate reasons for extracting the raw pointer from a smart pointer. But by not using get() in general, the times that it is used becomes a red flag for "there's something funky going on here, pay attention!".

Andre Kostur
  • 770
  • 1
  • 6
  • 15