Ask Your Question

Returning a variables form a callback function C++

asked 2019-09-19 01:17:38 -0600

drodgu gravatar image

Hi everyone,

I'm developing a TCP communication ROS node and I want to send a string to another ROS node. In this case, I'm using a std_msgs::String message to send information between this two nodes. The point is that when you do ROS Tutorial, Callback function uses a global variable in order to save the std_msgs::String value. I don't want to use a global variable because is not recomended while programming. Is there a way to use a pointer to a std::string or anything to get that value returned without using a global variable? It would be cool if there's a way without using a class like in this example 1, because I don't want to implement a whole class just to get a string value.

Here I leave that part of my ROS code:

void tcp_listener_Callback(const std_msgs::String::ConstPtr &tcp_msgs, std::string listener_return){
 listener_return = tcp_msgs->data.c_str();

and the ros subscriber callback:

ros::Subscriber tcp_listener_sub = nh.subscribe<std_msgs::String>("PLC_command_cmd",1000,boost::bind(tcp_listener_Callback,_1,sTCP_listener_cmd));
edit retag flag offensive close merge delete


There is no problem with using a global variable. If you are concerned about memory you can simply use a pointer as a global variable and clear the value. Otherwise the initialization of your subscriber tcp_listener_sub should be done once only (not in a loop!). So this means that the return string arg sTCP_listener_cmd can be used only once in this case which beats the purpose of the subscriber.

pavel92 gravatar image pavel92  ( 2019-09-19 02:29:48 -0600 )edit

Thanks, I know that there's no problem in using a global variable. But I don't like much programming using global vars. So I just wondering if there's a method to use another pointer to a string variable to use when you call your callback function and how to do it without declaring a whole class. But I've read on the internet that using a class is the best way to achieved this, so I think I'll try it.

drodgu gravatar image drodgu  ( 2019-09-19 02:43:01 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-09-19 09:23:33 -0600

drodgu gravatar image

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{
    void listener_rsp_callback(const std_msgs::String::ConstPtr &listener_msg);


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


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();
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



Asked: 2019-09-19 01:17:38 -0600

Seen: 1,072 times

Last updated: Sep 19 '19