Problems using Class Methods as Callbacks.
Hi All, I have a weird issue that I am facing while using class methods as callbacks. I am going to try and summarize the issue here.
class A{
protected:
int alpha;
}
class B:public A{
public:
B(){ alpha = 1; }
void some_func(){ alpha = 2; }
void odom_callback(const nav_msgs::Odometry::ConstPtr& msg){ std::cout<<alpha;}
}
int main(){
B * var = new B;
B->some_ func();
ros::spin();
}
Now, when the odom callback is called, I would expect it to print 2 for the value of alpha. However, in the callback it keeps printing 1. I know for sure that some_func()
has been called. However I do not know why in the callback it uses the first initialized value. Of course my real code is more complex, though I tried to illustrate the fault in this example. Please let me know what you think might be going wrong.
Thanks!
Ammar
Try making your example more realistic, i.e. a real node that can compile, with a real subscriber, etc. and test it. It should work as expected. Meaning that your real and more complex code has a different issue. But try this first.