게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C++ 콜백함수
게시물ID : computer_122639짧은주소 복사하기
작성자 : 황금
추천 : 0
조회수 : 784회
댓글수 : 2개
등록시간 : 2013/10/27 13:21:13

구현부분
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();
}

제가 간단히 만들어본 콜백함수인데요

아는 정보 공유하고 개선점같은것이 있으면 수정도 해보고파 올려봐요
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호