Note that there are some explanatory texts on larger screens.

plurals
  1. POCast between function pointers
    primarykey
    data
    text
    <p>I am currently implementing a timer/callback system using Don Clugston's fastdelegates. (see <a href="http://www.codeproject.com/KB/cpp/FastDelegate.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/cpp/FastDelegate.aspx</a>)</p> <p>Here is the starting code:</p> <pre><code>struct TimerContext { }; void free_func( TimerContext* ) { } struct Foo { void member_func( TimerContext* ) { } }; Foo f; MulticastDelegate&lt; void (TimerContext*) &gt; delegate; delegate += free_func; delegate += bind( &amp;Foo::member_func, &amp;f ); </code></pre> <p>Okay, but now, i wish the user to be able to subclass <code>TimerContext</code> to store and send his own structures to the callbacks. The purpose here is to prevent the user from having to downcast the <code>TimerContext</code> himself</p> <pre><code>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&lt; void (TimerContext*) &gt; delegate; delegate += free_func; delegate += free_func2; delegate += bind( &amp;Foo::member_func, &amp;f ); delegate += bind( &amp;Foo::member_func2, &amp;f ); </code></pre> <p>As you guessed, GCC won't let me do that :)</p> <pre><code>error: invalid conversion from `void (*)(MyTimerContext*)' to `void (*)(TimerContext*)' error: initializing argument 1 of `delegate::Delegate&lt;R ()(Param1)&gt;::Delegate(R (*)(Param1)) [with R = void, Param1 = TimerContext*]' </code></pre> <p>So now my question is: If I force the cast using <code>reinterpret_cast</code>, it'll work, but will it be safe ?</p> <p>PS: These are time-critical callbacks, heavy virtual-oriented solutions are considered impracticable :/</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload