Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a generic method pointer of a base class to call methods of derived classes
    primarykey
    data
    text
    <p>I'm trying to create an event handling system where objects can subscribe to events broadcasted by other objects and call a method when that event is broadcast. I'm trying to implement this with a singleton class called <code>EventHandler</code>. Objects call a public method on <code>EventHandler</code> that subscribes them to events broadcast by a certain object:</p> <pre><code>void startWatchingObjectEvent(PDObject * object, PDEvent event, PDObject* listener, PDReceiver receiver); </code></pre> <p><code>PDObject</code> is a base class for all the objects that will be handled by the <code>EventHandler</code>. <code>PDEvent</code> is simply a unique string that identifies the Event. </p> <p><code>PDReceiver</code> is where I'm running into issues. I have it typedef'd to a method pointer on the <code>PDObject</code> class:</p> <pre><code>typedef void (PDObject::*PDReceiver)(PDObject*, PDEvent); </code></pre> <p>Now the <code>PDObject</code> class itself doesn't really have any methods that <code>EventHandler</code> is going to call, but derived classes of <code>PDObject</code> will, and I wanted to create a generic type that <code>EventHandler</code> could store that would apply to any derived classes of <code>PDObject</code>. I implemented <code>EventHandler</code> this way and tried writing a unit test to make sure it's working okay:</p> <pre><code>const PDEvent Test_PokeEvent = "Test_PokeEvent"; void Test_EventHandler_sendObjectMessage() { EventHandler&amp; eventh = EventHandler::instance(); Test_PDObject caster; Test_PDObject listener; PDReceiver receiver = &amp;Test_PDObject::test_poke; eventh.startWatchingObjectEvent(&amp;caster, Test_PokeEvent, &amp;listener, (listener.*receiver)(&amp;caster, Test_PokeEvent)); eventh.sendObjectEvent(&amp;caster, Test_PokeEvent); assert(listener.test_wasPoked()); } </code></pre> <p><code>Test_PDObject</code> is a derived class of <code>PDObject</code> that just flips a <code>wasPoked</code> bit when <code>test_poke</code> is called.</p> <p>The issue is that the compiler doesn't like me trying to assign a method of <code>Test_PDObject</code> to the type <code>PDReceiver</code> since <code>PDReceiver</code> is defined as a method of <code>PDObject</code>.</p> <p>Is what I'm trying to do even possible? Can I use this generic method pointer type to refer to methods of derived classes? Or is my approach just entirely flawed?</p> <p>EDIT: I figured out I can use <code>static_cast</code> to cast the method pointer of the derived class to a method pointer of the base class like so:</p> <pre><code>PDReceiver receiver = static_cast&lt;PDReceiver&gt;(&amp;Test_PDObject::test_poke); </code></pre> <p>And so far so good! My unit tests are working perfectly now.</p>
    singulars
    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.
 

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