Inhert from a ros message

asked 2020-01-24 09:26:02 -0500

I am trying to make a class that publicy inherits from a ros message type. The goal of this is to extend functionality by providing an interface ontop of the message.

So for example:

// MyMsg in package my_msgs
uint8 a
uint8 b

// IN another file/package
struct MyNewMsg : public my_msgs::MyMsg {
  uint16_t GetSum() { return a + b;}
}; // end of MyNewMsg

// Later on I want to publish MyNewMsg
MyNewMsg t;
t.a = 1;
t.b = 2;
my_pub.publish(t)
// Or subscribe to MyNewMsg

However I get errors that MyNewMsg does not have the serialize functions for ROS messages. I was hoping I would get those functions by publicly inherting from MyMsg. I have not added new members to MyNewMsg, just functions.

edit retag flag offensive close merge delete

Comments

I would not do this. The message headers are not meant to be used this way.

If you want to add custom functionality (which is a bit of an anti-pattern actually), you could perhaps look at wiki/roscpp/Overview/MessagesSerializationAndAdaptingTypes: Customizing generated message headers for C++.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-24 10:28:38 -0500 )edit