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