私は現在、DonClugstonの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:これらはタイムクリティカルなコールバックであり、重い仮想指向のソリューションは実行不可能と見なされます:/