C++을 사용하여 게임 코드를 작성하고 있습니다. Child
의 멤버 함수를 delegate에 바인딩하고 싶습니다.상속받은 클래스의 멤버 Functor 전달
나는이 간단한 코드처럼 init_and_bind
기능을 사용하려면 : 나는 Widget
클래스에서 init_and_bind
을 구현하려는
class Parent {
protected:
Widget* widget;
};
class Child : public Parent {
public:
void foo() {
widget->init_and_bind(this, &Child::bar);
}
void bar() { /* do something */ }
};
, 그래서 나는 코드를 다음과 같이 구현 :
// pre-defined in engine
void Delegate::bind(Parent* object, void(Parent::* function)());
void Widget::init_and_bind(Parent* object, void(Parent::* function)()) {
init();
delegate->bind(object, function);
}
그러나 그렇지 않습니다 작업. init_and_bind
의 두 번째 매개 변수는 부모의 구성원 함수 유형 만 허용하기 때문입니다. 그래서 나는 Child의 멤버 함수를 전달할 수 없다. 그래서 템플릿과 reinterpret_cast
을 사용하려고했습니다 :
그러나 작동하지 않습니다. Child
의 펑터를 Parent
의 펑터로 캐스팅하지 못했기 때문입니다.
그래서 어떤 유형이 init_and_bind
의 두 번째 인수 여야합니까?
'static_cast'가 아니라 'reinterpret_cast'입니다. – StoryTeller
@StoryTeller 오, 감사합니다! 하지만 왜'reiniterpret_cast'가 컴파일되지 않았는지 모르겠습니다. –
다른 작업에 대해 다른 캐스트. 컴파일러는 올바른 것을 사용하고 있는지 확인합니다. 만약 당신이 전혀 캐스팅 할 필요가 있다면 기본적으로'static_cast'로 시작하십시오. – StoryTeller