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?
Practically no difference.
In case of
*p, the overloadedoperator*(defined byauto_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 asstd::unique_ptrandstd::shared_ptr(along withstd::weak_ptr).