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

Why to use std::bind to declare the callback function

asked 2021-03-16 17:16:55 -0600

pablo.arandarod gravatar image

I'm learning about ROS2, and I was creating my own project, and I cannot understand why, when the callback function is declared in the wall timer, this one has to be declared with std::bind.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-03-24 14:53:52 -0600

CroCo gravatar image

updated 2023-05-23 09:22:48 -0600

This has to do with C++. Usually, when you create a class, you want to encapsulate your data inside the class. The creation of callback functions as member data in a class can be tricky and problematic. A multitude of C libraries contain callback functions that allow one to access movement of the mouse or keyboard, for example. SDL2, Freeglue, SFML,... Etc., are such examples. Callback functions are ubiquitous in C programming; however, there is no concept for OOP in C. C++ tries to be fully compatible with C codes. It is possible to treat callback functions as member data in C++ using std::bind(), which is one of many solutions. You can search for this since it is a common issue when using C libraries.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-03-16 17:16:55 -0600

Seen: 1,274 times

Last updated: May 23 '23