• Advertisement
Sign in to follow this  

Update system with pointer to members

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is what I can do:
class A
{
   public:
      virtual void Update( int n ) { };
};

class B : public A
{
   public:
      virtual void UpdateNetwork( int n ) { };
}

class C : public A
{
   public:
      virtual void UpdateAnimation( int n ) { };
      virtual void UpdateLights( int n ) { };
}


typedef void (A::*tmm)(int n);

int main()
{
   list< pair<tmm, A*> > lCallBacks;

    B* b = new B;
       lCallBacks.push_bak( make_pair(&A::UpdateNetwork, b) );

    C* c = new C;
       lCallBacks.push_bak( make_pair(&A::UpdateAnimation, c) );
       lCallBacks.push_bak( make_pair(&A::UpdateLights, c) );

   // now the fun:
   for ( list< pair<tmm, A*> >::iterator iter( lCallBacks.begin() ); iter!=lCallBakcs.end(); ++iter)
      iter->second->*(iter->first)( 3 );
}

That way, I can have an updater system that is able to call update functions from any class that inherit from A. The good thing is that the classes can register any update funtion as far as the functions returns nothing and accept an integer as parameter. This is nice for me since some of my objects has functions to update the network but also to update the animations, etc. This way I can register in an scheduler and use the callbacks to call the appropiate functions seamlessly. Now my question: How to remove the need for the classes to inherit from A. Is it possible to use some kind of reinterprect_cast or similar to avoid the inheritance?

Share this post


Link to post
Share on other sites
Advertisement
boost::function.

You'd use boost::function<void(int)> as function pointer. Then you can register any member or non-member function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
boost::function.

You'd use boost::function<void(int)> as function pointer. Then you can register any member or non-member function.


And Boost.Bind.

Those two combined are the best thing to happen to C++ since sliced bread and Boost.SharedPtr.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement