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

Revision history [back]

click to hide/show revision 1
initial version

The best way to to it, is by declaring a class where you can instantiate an object from it. This way, instead of using a standard callback you use a pointer callback and the variables to manipulate are the private atributes of the class. This way, using getters you can easily return the variables you need.

If anyone needs it, here I left some part of my code: my_class.h

class my_class{
  private:
    ...
  public:
    ...
    void listener_rsp_callback(const std_msgs::String::ConstPtr &listener_msg);
    ...
  protected:
};

my_class.cpp

...
void my_class::listener_rsp_callback(const std_msgs::String::ConstPtr &listener_msg){
  sListener = listener_msg->data.c_str();
};
...

my_node.cpp

...
int main(int argc, char **argv){
...
 my_class cObject;
...
ros::Subscriber listen_sub = nh.subscribe<std_msgs::String>("t_name",1000,&my_class::listener_rsp_callback,&cObject);
std::string info = cObject.getString();
...
}