fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. // Macros make the call much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.6
  5. #define CALL_MEMBER_FN(object, ptrToMember) ((object).*(ptrToMember))
  6. template <class T>
  7. struct Second
  8. {
  9. // Typedefs make this much more readable: http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.5
  10. typedef void (T::*SecondTriggeredCallback)(void);
  11.  
  12. // You'll also need an *instance* of the "T" class
  13. Second(SecondTriggeredCallback SecondTriggered, T& t)
  14. {
  15. CALL_MEMBER_FN(t, SecondTriggered)();
  16. }
  17. };
  18.  
  19. class First
  20. {
  21. public:
  22. First()
  23. :second(nullptr)
  24. {
  25. std::cout << "first class was created" << std::endl;
  26. second.reset(new Second<First>(&First::SecondTriggered, *this));
  27. }
  28.  
  29. void SecondTriggered()
  30. {
  31. std::cout << "second class was created and responded" << std::endl;
  32. }
  33.  
  34. private:
  35. std::unique_ptr<Second<First>> second;
  36. };
  37.  
  38. int main()
  39. {
  40. First first;
  41. }
  42.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
first class was created
second class was created and responded