함수 객체 (Functor) : 함수처럼 동작하는 객체
함수 포인터의 단점
함수 시그니처 같아야 함.
상태를 가질 수 없다.
함수 포인터 선언
void (*pfunc)(void);
pfunc = &HelloWorld;
(*pfunc)();
class Functor
{
public:
void operator()()
{
cout << "Functor test" << endl;
cout << _value << endl;
}
bool operator()(int num)
{
_value += num;
return true;
}
private:
int _value = 0;
};
Functor functor;
functor();
bool ret = functor(3);
MMO 에서 함수 객체를 사용하는 예시
클라 <-> 서버
서버 : 클라가 보내준 네트워크 패킷을 받아서 처리
ex) 클라 : 나 (5, 0) 좌표로 이동시켜줘!
class MoveTask
{
public:
void operator()()
{
// TODO
}
public:
int _playerId;
int _posX;
int _posY;
};
MoveTask task;
task._playerId = 100;
task._posX = 5;
task._posY = 0;
나중에 여유될 때 일감을 실행한다.
task();