fork(4) download
  1. #include <iostream>
  2.  
  3. template <class T>
  4. class Logger
  5. {
  6. T value;
  7.  
  8. public:
  9.  
  10. T& operator=(const T& other)
  11. {
  12. std::cout << "Setting new value\n";
  13. value = other;
  14. return value;
  15. }
  16.  
  17. operator T() const
  18. {
  19. return value;
  20. }
  21.  
  22. };
  23.  
  24. class Foo
  25. {
  26. public:
  27. Foo() {}
  28.  
  29. void SetBar(int value)
  30. {
  31. //Log that m_bar is going to be changed
  32. m_bar = value;
  33. }
  34.  
  35. private:
  36.  
  37. #if 1
  38. Logger<int> m_bar; // the variable we want to track
  39. #else
  40. int m_bar; // the variable we want to track
  41. #endif
  42.  
  43. };
  44.  
  45. int main()
  46. {
  47. auto f = Foo();
  48. f.SetBar(12);
  49. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Setting new value