구현부분
class IFunctionPtr
{
public:
virtual void Func() = 0;
protected:
IFunctionPtr() {}
};
template<class T, typename F>
class FunctionPtr : public IFunctionPtr
{
public:
~FunctionPtr() {}
virtual void Func()
{
(_obj->*_func)();
}
template<class T, typename F>
friend IFunctionPtr* CreateFuncPtr(T* object, F function);
private:
FunctionPtr(T* object, F function)
: _obj(object), _func(function) {}
T* _obj;
F _func;
};
template<class T, typename F>
IFunctionPtr* CreateFuncPtr(T* object, F function) {
return new FunctionPtr(object, function);
}
사용부분
class TestFunction
{
public:
TestFunction()
{
m_pFuncPtr = CreateFuncPtr(this, &TestFunction::CallbackFunction);
}
~TestFunction()
{
SAFE_DEL(m_pFuncPtr)
}
public:
void operator() () const
{
m_pFuncPtr->Func();
}
void CallbackFunction()
{
cout << "Callback!!" << endl;
}
private:
IFunctionPtr* m_pFuncPtr;
};
void main()
{
TestFunction test;
test();
}
제가 간단히 만들어본 콜백함수인데요
아는 정보 공유하고 개선점같은것이 있으면 수정도 해보고파 올려봐요