Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should read <a href="https://stackoverflow.com/questions/3381829/how-do-i-implement-a-callback-in-c">How do I implement a callback in C++?</a> and pay particular attention to references to the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer">Observer Pattern</a>. If you have two such tightly-coupled classes then you probably want to re-think your design as testing them will soon become a nightmare.</p> <p>That said, here is how to finish the implementation that you started...</p> <pre><code>#include &lt;iostream&gt; class First; // Typedefs make this much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5 typedef void (First::*SecondTriggeredCallback)(void); // And macros make the call much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.6 #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember)) class Second { public: // You'll also need an *instance* of the First class Second(SecondTriggeredCallback SecondTriggered, First&amp; first) { CALL_MEMBER_FN(first, SecondTriggered)(); } }; class First { private: Second *second; public: First() { std::cout &lt;&lt; "first class was created" &lt;&lt; std::endl; second = new Second(&amp;First::SecondTriggered, *this); } ~First() { delete second; } void SecondTriggered() { std::cout &lt;&lt; "second class was created and responded" &lt;&lt; std::endl; } }; int main() { First first; } </code></pre> <p><a href="http://ideone.com/7USO1" rel="nofollow noreferrer">See it run</a>!</p> <hr> <p>Here's a version that removes the coupling by using templates:</p> <pre><code>#include &lt;iostream&gt; // Macros make the call much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.6 #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember)) template &lt;class T&gt; struct Second { // Typedefs make this much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5 typedef void (T::*SecondTriggeredCallback)(void); // You'll also need an *instance* of the "T" class Second(SecondTriggeredCallback SecondTriggered, T&amp; t) { CALL_MEMBER_FN(t, SecondTriggered)(); } }; class First { public: First() :second(NULL) { std::cout &lt;&lt; "first class was created" &lt;&lt; std::endl; second = new Second&lt;First&gt;(&amp;First::SecondTriggered, *this); } ~First() { delete second; } void SecondTriggered() { std::cout &lt;&lt; "second class was created and responded" &lt;&lt; std::endl; } private: First(const First&amp;); First&amp; operator =(const First&amp;); Second&lt;First&gt;* second; }; int main() { First first; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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