dereferencing boost::shared_ptr<>

D

Dennis Jones

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
 
J

Joe Gottman

Dennis Jones said:
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
 
D

Dennis Jones

Joe Gottman said:
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.

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
 
Joined
Feb 19, 2013
Messages
1
Reaction score
0
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]);
}

######################################
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top