ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Confusion between ConstPtr vs Ptr

asked 2020-12-20 03:26:34 -0500

updated 2020-12-20 03:51:56 -0500

I have recently switched from Python to C++. I was going through the ROS Subscriber tutorials. I came across this piece of code -

void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

I wanted some clarity on ConstPtr.

As per my understanding, we are passing a reference to the msg variable in the chatterCallback function, where in, the msg variable itself is a pointer to a std_msgs::String object.

Assuming my (aforementioned) understanding is correct, why are we using the keyword ConstPtr?

Is it because the value of the msg variable isn't changing (i.e. it is always pointing to the same std_msgs::String object) or it is because the value of the std_msgs::String object (to which the msg variable is pointing to) isn't changing?

I went through this discussion on the forum. But, I am still confused.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-12-20 20:24:03 -0500

tryan gravatar image

A ConstPtr is a pointer that points to a constant object. In this case, the message itself (std_msgs::String object) is constant. In general, you wouldn't want to change the message's data, just calculate other quantities based on it.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-12-20 03:26:34 -0500

Seen: 798 times

Last updated: Dec 20 '20