C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

dereferencing boost::shared_ptr<>

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Dennis Jones
Guest





PostPosted: Thu Jul 28, 2005 12:08 am    Post subject: dereferencing boost::shared_ptr<> Reply with 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;

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





PostPosted: Thu Jul 28, 2005 1:05 am    Post subject: Re: dereferencing boost::shared_ptr<> Reply with quote




"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
Smile 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





PostPosted: Thu Jul 28, 2005 1:20 am    Post subject: Re: dereferencing boost::shared_ptr<> Reply with quote




"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
Smile 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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.