현재 Don Clugston의 fastdelegates를 사용하는 타이머/콜백 시스템을 구현 중입니다.함수 포인터 간 변환
여기 시작 코드 (http://www.codeproject.com/KB/cpp/FastDelegate.aspx 참조)
struct TimerContext
{
};
void free_func(TimerContext*)
{
}
struct Foo
{
void member_func(TimerContext*)
{
}
};
Foo f;
MulticastDelegate< void (TimerContext*) > delegate;
delegate += free_func;
delegate += bind(&Foo::member_func, &f);
좋아,하지만 지금, 나는 사용자가 저장하고 콜백에 자신의 구조를 보내 TimerContext
를 서브 클래 싱 할 수 있도록 바랍니다. 여기 목적은 당신이 짐작 이름으로 TimerContext
자신
struct TimerContext
{
};
struct MyTimerContext : TimerContext
{
int user_value;
};
void free_func(TimerContext*)
{
}
void free_func2(MyTimerContext*)
{
}
struct Foo
{
void member_func(TimerContext*)
{
}
void member_func2(MyTimerContext*)
{
}
};
Foo f;
MulticastDelegate< void (TimerContext*) > delegate;
delegate += free_func;
delegate += free_func2;
delegate += bind(&Foo::member_func, &f);
delegate += bind(&Foo::member_func2, &f);
를 다운 캐스트 할 필요에서 사용자를 방지하는 것입니다, GCC는 나를
error: invalid conversion from `void (*)(MyTimerContext*)' to `void (*)(TimerContext*)'
error: initializing argument 1 of `delegate::Delegate<R()(Param1)>::Delegate(R (*)(Param1)) [with R = void, Param1 = TimerContext*]'
그래서 지금은 내 질문은 : 그렇게 못하게 :
reinterpret_cast
을 사용하여 강제로 캐스트하면 제대로 작동하지만 안전할까요?
PS :이 시간에 중요한 콜백은 무거운 가상 중심의 솔루션은 불가능한 것으로 간주됩니다입니다 :/
Howw는 괜찮 :(는 참조 주셔서 감사합니다! TimerContext * 암시 MyTimerContext으로 변환하지 않기 때문에 – NewbiZ
심지어 부스트 : : 함수가 자신의 경우에 작동하지 않습니다 *. 나는이 유형의 안전 문제를 언급 내 Answer 그러나 어떤 이유로 든 downvoted되었습니다. – sellibitze