플밍

기초적인 클래스함수 콜백처리 방법 본문

프로그래밍/C/C++

기초적인 클래스함수 콜백처리 방법

너구리안주 2013. 10. 14. 16:53

c++에서 템플릿을 사용하지 않고 일반적으로 콜백을 처리하는 방법입니다.

누구나 다 아는 거지만 내가 헤깔려서 기록함 ㅠㅠ


#ads_1

CCObject 나 CCLog 등의 함수는 cocos2d-x의 객체입니다.


class CallClass{
public:
    typedef void (CCObject::* finHandler)(int); //콜백 클래스함수포인터 타입지정


private:

finHandler onFinHandler; //콜백함수
CCObject* finTarget; //콜백대상객체

//콜백 등록용 함수
void setFinHandler(CCObject* target, finHandler handler){
    finTarget = target;
    onFinHandler = handler;
}

void doProc(){
    if(onFinHandler != NULL) (finTarget->*onFinHandler)(123); //콜백이 등록되어 있으면 콜백실행(괄호 유의)
}

};

 

class Tester: public CCObject{
private:

CallClass* cc;


void test(){
    //콜백등록
    cc->setFinHandler(this, (CallClass::finHandler)(&Tester::onFinHandler) );
}

void onFindHandler(int n){

CCLog("%d", n);  //결과 "123"이 출력됨

}


public:

Tester(){

cc = new CallClass();
cc->retain();

}

virtual ~Tester(){

cc->release();

}

}

 

void main(){
    Tester t;
    t.test();
}



#ads_1

Comments