Update system with pointer to members

Started by
1 comment, last by _goat 16 years ago
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?
Advertisement
boost::function.

You'd use boost::function<void(int)> as function pointer. Then you can register any member or non-member function.
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.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]

This topic is closed to new replies.

Advertisement