I assume this
is e. g. of type MyDerivedClass*
where MyDerivedClass
inherits from MyClass
and the method StageOdom_callback
of class MyClass
is protected
or public
(not private
!).
If so have you tried:
ros::Subscriber StageOdo_sub = n.subscribe("robot_0/odom",1000, &MyClass::StageOdom_callback, dynamic_cast<MyClass*>( this ) );
If your compiler does not provide dynamic_cast
you could also use reinterpret_cast<MyClass*>
. However, dynamic_cast
is safer as it checks at runtime that the class of the object pointed to by this really inherits from MyClass
...
Edit:
I just noticed: you do not need to explizitly select the template type (<nav_msgs::Odometry>
) like you do with advertise
. The Template type is implizitly selected from the callback argument type...
Originally posted by Wolf with karma: 7555 on 2014-08-22
This answer was ACCEPTED on the original site
Post score: 3
Original comments
Comment by falseazure on 2014-08-23:
Great answer, thank you! I appreciate the heads up about selecting the template type, too.
Comment by wdudzik on 2019-12-05:
Hello! I realize this is a pretty old topic but this was incredibly helpful to me when trying to create a timer using a parent callback. I was curious why you have to cast this. Can anyone shed some light on that?