   function pointer to derived objects?
  I am implementing a callback system but I have a problem:The CCallback-class holds functors to functions of TCallback-derived objects (see Add() of CCallback). But the cast formvoid (CTest::*)(void) to void (TCallbackable::*)(void) doesn'twork. What can I do?Please look at the (much simplified) source code: class TCallbackable {};class CCallback {...public:  void Add(const TCallbackable& rObj, void(TCallbackable::*fp)(void)) {  ...  }};//main prog:CTest : public TCallbackable {public:  void callit(void) { std::cout << "called!"; }};CSource : public CCallback {};main {  CSource src;  CTest test;    src.Add(test, CTest::callit);  //Here is the problem!!}Thanks!Constantin[edited by - conman on February 12, 2004 9:03:24 AM]
  What do you mean? I tryed a lot but I don''t know how to do it, that''s why I posted in this forum! ;  sorry I deleted my post ;  I''m no expert on function pointers, but i''ll give my 2 cents anyway ...Have you tried something like this? Basically i just added the "virtual void callit();" in your base class: class TCallbackable {};class CCallback {...public:  void Add(const TCallbackable& rObj, void(TCallbackable::*fp)(void)) {  ...  }  virtual void callit();};//main prog:CTest : public TCallbackable {public:  void callit(void) { std::cout << "called!"; }};CSource : public CCallback {};main {  CSource src;  CTest test;    src.Add(test, callit);  //Here is the problem!!}Hope that works, If not maybe this site will help http://www.function-pointer.org/ ;  @spacedude:Thanks for your reply! Adding a virtual function to the base class would have been a solution, but I want that the classes that are callable can be called back on any function they want.The ambition is to make it a little more flexible than the normal event listener architecture... ;  I would wrap the callback function in a functor, something like the following: class Functor{public:virtual int operator ()()=0;};template<class T>class ObjVoidFunctor : public Functor{protected:T *obj;typedef void (T::*funcType)();funcType func;public:ObjVoidFunctor(T *o, funcType f) { obj=o; func=f; }int operator ()() {(obj->*func)(); return NULL; }}; // then you would declare it something like this:class CCallBack {…void Add(Functor *func);…}class CTest {void DoSomething();}// and use it like this:CCallBack *callback->Add(new ObjVoidFunctor<CTest>(testObject, &CTest::DoSomething));Hope this Helps,SwiftCoder   Here's to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes, the ones who see things differently. [edited by - SwiftCoder on February 12, 2004 1:16:32 PM];  tried looking at boost::function ?  ;  if that still doesnt help:(void(TCallbackable::*)(void))&class::functionor#define METHOD(x) (void(TCallbackable::*)(void))&xMETHOD(class::function)works for me at least (given i didnt miss an important difference in your case or made a mistake when adjusting it) ;typedef void (CTest::*BaseCallback) ( void );typedef void (CCallbackable::*Callbackable) ( void );//now that we have some types defined, you are//free to cast between them however you want.//the vtable will deal with inheritance issues.//so you can write:src.Add( test, (BaseCallback) CTest.callit() );//and it should be happy. This is essentially what''s done//in my GUI system.//and yes, this entire thing is supposed to be in a source box.;  Thank you guys!!I changed my code to work the way swiftcoder suggested. There was not much to change but now it works! - Constantin 