Robotics StackExchange | Archived questions

Confusion between ConstPtr vs Ptr

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.

Asked by skpro19 on 2020-12-20 04:26:34 UTC

Comments

Answers

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.

Asked by tryan on 2020-12-20 21:24:03 UTC

Comments