What is void counterCallback(const std_msgs::Int32::ConstPtr& msg)?

In ROS Basics in 5 days, unit 4, the following program is used to subscribe to Laser data

C++ Program {4.2}: simple_topic_subscriber.cpp:

#include <ros/ros.h>
#include <std_msgs/Int32.h>

void counterCallback(const std_msgs::Int32::ConstPtr& msg)
  ROS_INFO("%d", msg->data);

int main(int argc, char** argv) {

    ros::init(argc, argv, "topic_subscriber");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("counter", 1000, counterCallback);
    return 0;
  1. Can someone tell me why const std_msgs::Int32::ConstPtr& is used? specifically the need for const and ConstPtr&? why have be opted for this?
  2. I am familiar with the pass by reference technique, where we use &variable to get the address of a variable,but what what is variable& as used in ConstPtr& ?
  3. What is boost ? I have seen this comeup now and then

Hi Joseph, let me answer this the best I can.

  1. The subscriber callback uses const std_msgs::Int32::ConstPtr&, to use the message received in a specific way, in a boost shared_ptr, which means you can store this message if needed without it getting deleted underneath and without copying the underlying data.

  2. ::ConstPtr&, according to this post,What is ConstPtr&? - ROS Answers: Open Source Q&A Forum, can be understood as a reference to a shared pointer to a constant message. It points to the allocated memory where the message is being stored.

  3. boost is a collection of C++ libraries. It includes the shared_ptr class that is used in that callback variable.

Hi @roalgoal , thanks for replying. I think you answered most of my questions.
@staff It would have been great if ConstPtr& and boost was introduced in the C++ basics course provided by Construct. This way users could have more in-depth knowledge on the concepts in future courses.