 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dennis Jones Guest
|
Posted: Thu Jul 28, 2005 12:08 am Post subject: dereferencing boost::shared_ptr<> |
|
|
Hello,
Given something like:
boost::shared_ptr<T> t( new T() );
What is the best (correct?) way to dereference the pointer? The following
two methods work. Is there a difference?
T &rt1 = *t.get();
T &rt2 = *t;
And what about getting at the raw pointers:
T *pt1 = t.get();
T *pt2 = &*t;
Is there an advantage to one or the other?
Thanks,
- Dennis
|
|
| Back to top |
|
 |
Joe Gottman Guest
|
Posted: Thu Jul 28, 2005 1:05 am Post subject: Re: dereferencing boost::shared_ptr<> |
|
|
"Dennis Jones" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Hello,
Given something like:
boost::shared_ptr<T> t( new T() );
What is the best (correct?) way to dereference the pointer? The following
two methods work. Is there a difference?
T &rt1 = *t.get();
T &rt2 = *t;
|
I prefer the second method, because you get to type four fewer characters
Also, suppose you have a typedef somewhere that looks like
typedef shared_ptr<T> TPtr;
In the second case, you maintain the option of changing this to
typedef T *TPtr;
without having to change any dereferencing code.
| Quote: | And what about getting at the raw pointers:
T *pt1 = t.get();
T *pt2 = &*t;
|
I definitely prefer the first case here. What is t.get() == 0. Then the
second case invokes undefined behavior by dereferencing a null pointer. For
example, the boost implementation of shared_ptr asserts that get() != 0
inside its implementations of operator*() and operator->().
Joe Gottman
|
|
| Back to top |
|
 |
Dennis Jones Guest
|
Posted: Thu Jul 28, 2005 1:20 am Post subject: Re: dereferencing boost::shared_ptr<> |
|
|
"Joe Gottman" <jgottman (AT) carolina (DOT) rr.com> wrote
| Quote: |
What is the best (correct?) way to dereference the pointer? The
following
two methods work. Is there a difference?
T &rt1 = *t.get();
T &rt2 = *t;
I prefer the second method, because you get to type four fewer characters
Also, suppose you have a typedef somewhere that looks like
typedef shared_ptr<T> TPtr;
|
Thank you. I agree with your assessment. I was just a little surprised
that the second one compiled and worked. I thought (perhaps naively) it
looked like I was dereferencing the shared_ptr instead of the underlying raw
pointer. But now that I look at the header file, I see that operator *() is
defined to return a reference to the pointee.
| Quote: | And what about getting at the raw pointers:
T *pt1 = t.get();
T *pt2 = &*t;
I definitely prefer the first case here. What is t.get() == 0. Then
the
second case invokes undefined behavior by dereferencing a null pointer.
For
example, the boost implementation of shared_ptr asserts that get() != 0
inside its implementations of operator*() and operator->().
|
I wouldn't have thought of that -- you're absolutely right.
Thanks for your insight,
- Dennis
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|