fork download
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5. int value = 1;
  6.  
  7. void printAddress(const char* funcName) const
  8. {
  9. std::cout << funcName << " called on object with address " << this << '\n';
  10. }
  11.  
  12. void setValue(int);
  13. int getValue() const;
  14.  
  15. void reset() ;
  16. };
  17.  
  18. A globalObject;
  19.  
  20. void A::setValue(int val)
  21. {
  22. printAddress("A::setValue");
  23. value = val;
  24. }
  25.  
  26. int A::getValue() const
  27. {
  28. printAddress("A::getValue");
  29. return value;
  30. }
  31.  
  32. void A::reset()
  33. {
  34. printAddress("A::reset");
  35. globalObject.setValue(value);
  36. setValue(1);
  37. }
  38.  
  39. int main()
  40. {
  41. A functionObject;
  42.  
  43. std::cout << "Address of globalObject: " << &globalObject << '\n' ;
  44. std::cout << "Address of functionObject: " << &functionObject << '\n' ;
  45.  
  46.  
  47. functionObject.setValue(42);
  48. std::cout << functionObject.getValue() << '\n';
  49. std::cout << globalObject.getValue() << '\n';
  50.  
  51. functionObject.reset() ; // also manipulates global object.
  52.  
  53. std::cout << functionObject.getValue() << '\n';
  54. std::cout << globalObject.getValue() << '\n';
  55. }
  56.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Address of globalObject: 0x8049154
Address of functionObject: 0xbfdc9e3c
A::setValue called on object with address 0xbfdc9e3c
A::getValue called on object with address 0xbfdc9e3c
42
A::getValue called on object with address 0x8049154
1
A::reset called on object with address 0xbfdc9e3c
A::setValue called on object with address 0x8049154
A::setValue called on object with address 0xbfdc9e3c
A::getValue called on object with address 0xbfdc9e3c
1
A::getValue called on object with address 0x8049154
42