By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

dereferencing boost::shared_ptr<>

P: n/a
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
Jul 28 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Dennis Jones" <no****@nospam.com> wrote in message
news:11*************@corp.supernews.com...
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.
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
Jul 28 '05 #2

P: n/a

"Joe Gottman" <jg******@carolina.rr.com> wrote in message
news:Vb*********************@twister.southeast.rr. com...

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.

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
Jul 28 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.