void chatterCallback(const std_msgs::String::ConstPtr& msg)
{ROS_INFO("I heard: [%s]", msg->data.c_str());}
When messages are automatically generated into C++ code, there are several typedefs defined. One of them is ::Ptr
, which is typedef-ed to be a boost::shared_ptr<MSG>
, and another is ::ConstPtr
which is boost::shared_ptr<MSG const>
.
By passing a const pointer into the callback, we avoid doing a copy. While this might not make much difference for std_msgs::String
, it can make a huge difference for sensor_msgs::PointCloud2
.
https://answers.ros.org/question/212857/what-is-constptr/