Ask Your Question

Problems using Class Methods as Callbacks.

asked 2012-12-07 10:05:50 -0500

Ammar gravatar image

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{
   int alpha;
class B:public A{ 
   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();

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

edit retag flag offensive close merge delete



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.

brice rebsamen gravatar image brice rebsamen  ( 2012-12-07 11:54:09 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-12-07 12:40:02 -0500

I assume you meant:

B * var = new B;
var->some_ func();

ant not

B * var = new B;
B->some_ func();

As brice suggested, post the real example which is failing, or a link to it and a description of the relevant lines.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-12-07 10:05:50 -0500

Seen: 151 times

Last updated: Dec 07 '12