Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need a pointer-to-function:</p> <pre><code>void* (*OnJoinFn)(char*, char*); </code></pre> <p>In your <code>Irc</code> class,</p> <pre><code>class Irc { public: OnJoinFn onJoin; }; </code></pre> <p>This can be assigned as you are doing above:</p> <pre><code>int main() { Irc irc(stuff); irc.onJoin = join; } </code></pre> <p>But I wonder, if you are just learning C++, do you really need a pointer-to-function? pointers-to-function are certianly legal and valid, but an unusual entity and I would typically expect to use some other mechanism. As a start, I would suggest looking in to abstract base classes:</p> <pre><code>class IIrc { public: virtual void* OnJoin(const char*, const char*) = 0; // pure virtual virtual ~IIrc() {}; // Don't forget to implement a virtual destructor in any ABC }; class MyIrc : public IIrc { public: void* OnJoin(const char* sender, const char* channel*) { // YOUR CODE HERE } }; int main() { IIrc* irc = new MyIrc; irc-&gt;OnJoin (...); } </code></pre> <p>I've taken the liberty of introducing const correctness in <code>OnJoin</code>.</p> <p>You should also consider not returning a <code>void*</code>, which bypasses most of C++'s type safety mechanisms, but a pointer to an actual object, or another interface.</p> <p>Finally, using <code>new</code> (and <code>delete</code>, which is missing here, resulting in a memory leak) is poor practice. Instead, prefer to allocate things on the stack or, if you really need dynamic allocation, use a smart pointer.</p>
 

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