fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class SomeClass
  5. {
  6. private:
  7. int i = 0;
  8.  
  9. public:
  10. SomeClass() { cout << "default constructor\n"; };
  11. SomeClass(int val) { i = val; cout << "constructor getting int: " << val << '\n'; };
  12. ~SomeClass() { cout << "destrucing object having i: " << i << '\n'; };
  13.  
  14. SomeClass& operator=(const SomeClass& rhs) {
  15. cout << "operator= getting int: " << rhs.i << '\n';
  16. if (this != &rhs) {
  17. i = rhs.i;
  18. }
  19. return *this;
  20. }
  21. };
  22.  
  23. int main() {
  24. SomeClass a(10);
  25. SomeClass b = SomeClass(20);
  26. SomeClass c(35);
  27. c = SomeClass(46);
  28. return 0;
  29. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
constructor getting int: 10
constructor getting int: 20
constructor getting int: 35
constructor getting int: 46
operator= getting int: 46
destrucing object having i: 46
destrucing object having i: 46
destrucing object having i: 20
destrucing object having i: 10