fork download
  1. #include <iostream>
  2.  
  3. const int * funct(const int &x) { return &x; }
  4.  
  5. struct Integer {
  6. int value;
  7. Integer(const int &value) : value(value) { std::cout << "Created: " << this << std::endl; }
  8. ~Integer() { std::cout << "Destroyed: " << this << std::endl; }
  9. };
  10.  
  11. const Integer * funct2(const Integer &x) { return &x; }
  12.  
  13. int main()
  14. {
  15. int a = 3, b = 4;
  16.  
  17. // different addresses
  18. std::cout << funct(a) << std::endl;
  19. std::cout << funct(b) << std::endl;
  20.  
  21. // same address
  22. std::cout << funct(3) << std::endl;
  23. std::cout << funct(4) << std::endl;
  24.  
  25. // different addresses
  26. std::cout << funct(3) << std::endl << funct(4) << std::endl;
  27.  
  28. std::cout << "----------" << std::endl;
  29.  
  30. // same address
  31. std::cout << funct2(3) << std::endl;
  32. std::cout << funct2(4) << std::endl;
  33.  
  34. // different addresses
  35. std::cout << funct2(3) << std::endl << funct2(4) << std::endl;
  36. }
Success #stdin #stdout 0s 4728KB
stdin
Standard input is empty
stdout
0x7ffd6c0d8da0
0x7ffd6c0d8da4
0x7ffd6c0d8db4
0x7ffd6c0d8db4
0x7ffd6c0d8db0
0x7ffd6c0d8db4
----------
Created: 0x7ffd6c0d8db4
0x7ffd6c0d8db4
Destroyed: 0x7ffd6c0d8db4
Created: 0x7ffd6c0d8db4
0x7ffd6c0d8db4
Destroyed: 0x7ffd6c0d8db4
Created: 0x7ffd6c0d8dac
0x7ffd6c0d8dac
Created: 0x7ffd6c0d8db4
0x7ffd6c0d8db4
Destroyed: 0x7ffd6c0d8db4
Destroyed: 0x7ffd6c0d8dac