Original Post
Hi
i was trying to write in c++ a class that calls (callback) a member function and passes parameter, and other that doesnt need a parameter.
here's the code that seems to work but i got some possible heap corruption possibly because of it..
thx for taking a look! please tell me if its good or not and could it cause heap corruption?
i was trying to write in c++ a class that calls (callback) a member function and passes parameter, and other that doesnt need a parameter.
here's the code that seems to work but i got some possible heap corruption possibly because of it..
thx for taking a look! please tell me if its good or not and could it cause heap corruption?
class CallMemFnBase{public: virtual bool call(void * obj) = 0;};template <class T,class T2> class CallMemFn1: public CallMemFnBase{protected: typedef bool (T2::*Fn)(); Fn mFunc;public: CallMemFn1(Fn func): mFunc(func) {} bool call(void * obj) { return ((static_cast<T2*>(obj))->*mFunc)(); }};template <class T,class A> class CallMemFn2: public CallMemFnBase{ typedef bool (T::*Fn)(A a); Fn mFunc; A arg;public: CallMemFn2( Fn fn, A arg): mFunc(fn), arg(arg) {} bool call(void * obj) { return ((static_cast<T*>(obj))->*mFunc)(arg); }};// the pass it to function like this:&CallMemFn2<Entity,S32>(&Entity::isEnemy, getSide())// or like this:&CallMemFn1<Entity,Item>( &Item::isAvailable ) // to function , which takes an argumend like this: CallMemFnBase *callb = 0// and calls it:(*callb).call(entity)