dereferencing boost::shared_ptr<>

Discussion in 'C++' started by Dennis Jones, Jul 28, 2005.

  1. Dennis Jones

    Dennis Jones Guest

    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
    Dennis Jones, Jul 28, 2005
    #1
    1. Advertising

  2. Dennis Jones

    Joe Gottman Guest

    "Dennis Jones" <> wrote in message
    news:...
    > 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
    Joe Gottman, Jul 28, 2005
    #2
    1. Advertising

  3. Dennis Jones

    Dennis Jones Guest

    "Joe Gottman" <> wrote in message
    news:VbWFe.66891$...
    > >
    > > 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
    Dennis Jones, Jul 28, 2005
    #3
  4. Dennis Jones

    João Martins

    Joined:
    Feb 19, 2013
    Messages:
    1
    hi everyone, my name is João and i'm working on map merge for multi-robot slam. I'm testing on ROS and i found this topic about dereferencing boost::shared_prt<>. i want to do alignment of 2 parcial maps to one global map. The parcial maps are subscribed from robot_0/map and robot_1/map... my problem i think is in the callback function, because the type is (const nav_msgs::OccupancyGrid::ConstPtr& mp_0) and i m trying to get with a (boost::shared_ptr<nav_msgs::OccupancyGrid>). i already spend much time with this without success... i hope someone could give me a solution for this ;)

    ######################## CODE #####################################
    using std::vector;
    using boost::assign::eek:perator+=;
    using occupancy_grid_utils::Cell;

    typedef vector<Cell> Path;
    typedef boost::shared_ptr<nav_msgs::OccupancyGrid> GridPtr;
    typedef boost::shared_ptr<nav_msgs::OccupancyGrid const> GridConstPtr;

    //GridPtr g0, g1;

    GridPtr g0(new nav_msgs::OccupancyGrid());
    GridPtr g1(new nav_msgs::OccupancyGrid());
    //GridConstPtr GridPtr_0, GridPtr_1;


    /**
    * CALLBACK para receber os mapas!
    */
    void mp_0Callback(const nav_msgs::OccupancyGrid::ConstPtr& mp_0){
    //ROS_INFO("CELL=%d",mp_0->data[0]);
    //Grid_0 = *mp_0;

    //GridPtr_0 = mp_0;
    g0 = boost::const_pointer_cast<nav_msgs::OccupancyGrid>(mp_0);
    //ROS_INFO("CELL(0,r0)=%d",GridPtr_0->data[0]);
    }

    void mp_1Callback(const nav_msgs::OccupancyGrid::ConstPtr& mp_1){
    //GridPtr_1 = mp_1;
    g1 = boost::const_pointer_cast<nav_msgs::OccupancyGrid>(mp_1);
    //ROS_INFO("CELL(0,r1)=%d",GridPtr_1->data[0]);
    }

    ######################################
    João Martins, Feb 19, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philippe Guglielmetti
    Replies:
    4
    Views:
    882
    tom_usenet
    Oct 9, 2003
  2. James Mastro

    boost::shared_ptr inside stl::list

    James Mastro, Nov 13, 2003, in forum: C++
    Replies:
    1
    Views:
    2,011
    Cy Edmunds
    Nov 13, 2003
  3. Derek
    Replies:
    2
    Views:
    615
    Derek
    Dec 8, 2003
  4. Toby Bradshaw
    Replies:
    6
    Views:
    1,712
    Kai-Uwe Bux
    Jun 2, 2006
  5. Colin Caughie
    Replies:
    1
    Views:
    690
    Shooting
    Aug 29, 2006
Loading...

Share This Page