Ask Your Question
0

About listener.cpp tutorial. disobey cpp grammar?

asked 2017-02-20 18:25:17 -0500

Bill5785 gravatar image
 #include "ros/ros.h"
#include "std_msgs/String.h"

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

    int main(int argc, char **argv)
    {
      ros::init(argc, argv, "listener");

      ros::NodeHandle n;

      ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

      ros::spin(); 

      return 0;
    }

there is no actual parameter passing to chatterCallback function?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-02-20 21:08:12 -0500

ahendrix gravatar image

chatterCallback is used a function pointer in this context, so the function isn't being called by advertise(). Instead, the address of the function is stored and used later to call that function every time a new message arrives.

Here's a wikipedia article about function pointers and another article specifically about using function pointers in C++ if you want to learn more about the details.

edit flag offensive delete link more

Comments

Thanks! I found I was stupid and forgot the function pointers...

Bill5785 gravatar image Bill5785  ( 2017-02-20 21:29:40 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-02-20 18:25:17 -0500

Seen: 35 times

Last updated: Feb 20 '17