假设你有一个简单的类:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/listener_class/listener_class.cpp
class Listener
{
public:
void callback(const std_msgs::String::ConstPtr& msg);
};
当NodeHandle::subscribe()调用回调函数时类似于这样:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/listener/listener.cpp
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
当回调函数是类的一个方法时:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/listener_class/listener_class.cpp
Listener listener;
ros::Subscriber sub = n.subscribe("chatter", 1000, &Listener::callback, &listener);
If the subscriber is inside the class Listener, you can replace the last argument with the keyword this, which means that the subscriber will refer to the class it is part of.
假定你有一个简单的类:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/add_two_ints_server_class/add_two_ints_server_class.cpp
class AddTwo
{
public:
bool add(roscpp_tutorials::TwoInts::Request& req,
roscpp_tutorials::TwoInts::Response& res);
};
旧的odeHandle::advertiseService() 回调函数可能是这个样子:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/add_two_ints_server/add_two_ints_server.cpp
ros::ServiceServer service = n.advertiseService("add_two_ints", add);
类版本的可能是这个样子:
https://raw.github.com/ros/ros_tutorials/groovy-devel/roscpp_tutorials/add_two_ints_server_class/add_two_ints_server_class.cpp
AddTwo a;
ros::ServiceServer ss = n.advertiseService("add_two_ints", &AddTwo::add, &a);
For the full set of subscribe(), advertiseService(), etc. overloads, see the NodeHandle class documentation