Note that there are some explanatory texts on larger screens.

plurals
  1. POHow I do define a class method that accepts a derived class member function pointer as a parameter in C++?
    primarykey
    data
    text
    <p>I'm using a Signal-Slot C++ implementation found here: <a href="https://github.com/pbhogan/Signals" rel="nofollow">https://github.com/pbhogan/Signals</a> to implement event listeners within a user interface library I'm developing.</p> <p>The user-facing API goes like so:</p> <pre><code>class DialEventListener : public EventListener { void handleEvent(Event event){ … cout &lt;&lt; "Event on" &lt;&lt; event.source.name &lt;&lt; end; } } int main(){ Interactor dial1; dial1.addListener(ROTATE_EVENT, DialEventListener()); … } </code></pre> <p>To define an event listener, a user would subclass <code>EventListener</code> to create a custom event handler. But in the implementation of <code>Interactor::addListener()</code> where I use the Signals.h code you have to provide a member function pointer as well as a class instance pointer to connect a Delegate to some Signal.</p> <p>Below is an code snippet showing how I'm trying to use Signals to implement event listeners. I use a map from <code>EventType</code>s to <code>Signal</code> to account for an interactor that triggers multiple types of events.</p> <pre><code>typedef map&lt; EventType, Signal1&lt;Event&gt; &gt; EventTypeListenerMap; class Interactor { private: EventTypeListenerMap listener_map; public: void TriggerEvent(Event e){ Signal1&lt;Event&gt; signal = listener_map[e.type]; signal(e); } void addListener(EventType e_type, EventListener listener){ … //Find the signal for a particular kind of event. Signal1&lt;TUIKitEvent&gt; signal = listener_map[e_type]; //Plug a delegate into that signal's slot signal.Connect( &amp;listener, &amp;EventListener::handleEvent); } void addListener(EventType e_type, EventListener listener, void (EventListener::*handler)(Event)){ … Signal1&lt;TUIKitEvent&gt; signal = listener_map[e_type]; signal.Connect( &amp;listener, handler); } } class EventListener { virtual void handleEvent(Event event){ } } </code></pre> <p>The fact that I'm using a Signal-Slot implementation should be hidden from the user, so I just wrote this version of <code>Interactor::addListener(EventType, EventListener, void (EventListener::*))</code> as a sanity check to make sure I was passing the right member function pointer to be bound to that signal. But member function pointers are class specific, so how can one define a function with a signatures that accepts pointers to methods of classes derived from EventListener, which likely don't yet exist?</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.
 

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