dereferencing boost::shared_ptr<>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis Jones

    dereferencing boost::shared_ptr<>

    Hello,

    Given something like:

    boost::shared_p tr<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


  • Joe Gottman

    #2
    Re: dereferencing boost::shared_p tr&lt;&gt;


    "Dennis Jones" <nospam@nospam. com> wrote in message
    news:11eg8g8r92 0s9f7@corp.supe rnews.com...[color=blue]
    > Hello,
    >
    > Given something like:
    >
    > boost::shared_p tr<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;
    >[/color]

    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.
    [color=blue]
    > And what about getting at the raw pointers:
    >
    > T *pt1 = t.get();
    > T *pt2 = &*t;
    >[/color]

    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


    Comment

    • Dennis Jones

      #3
      Re: dereferencing boost::shared_p tr&lt;&gt;


      "Joe Gottman" <jgottman@carol ina.rr.com> wrote in message
      news:VbWFe.6689 1$Kp2.3647815@t wister.southeas t.rr.com...[color=blue][color=green]
      > >
      > > What is the best (correct?) way to dereference the pointer? The[/color][/color]
      following[color=blue][color=green]
      > > two methods work. Is there a difference?
      > >
      > > T &rt1 = *t.get();
      > > T &rt2 = *t;
      > >[/color]
      >
      > 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;[/color]

      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.

      [color=blue][color=green]
      > > And what about getting at the raw pointers:
      > >
      > > T *pt1 = t.get();
      > > T *pt2 = &*t;
      > >[/color]
      >
      > I definitely prefer the first case here. What is t.get() == 0. Then[/color]
      the[color=blue]
      > second case invokes undefined behavior by dereferencing a null pointer.[/color]
      For[color=blue]
      > example, the boost implementation of shared_ptr asserts that get() != 0
      > inside its implementations of operator*() and operator->().[/color]

      I wouldn't have thought of that -- you're absolutely right.

      Thanks for your insight,

      - Dennis


      Comment

      Working...