fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int idx = 0;
  5.  
  6. class CTest {
  7. public:
  8. int x;
  9. int z;
  10. CTest(int x) {
  11. z = ++idx;
  12. cout << "Create " << x << ' ' << z << endl;
  13. this->x = x;
  14. };
  15.  
  16. CTest(const CTest & src) {
  17. z = ++idx;
  18. cout << "Copy " << src.x << ' ' << z << " <- " << src.z << endl;
  19. this->x = src.x;
  20. };
  21.  
  22. ~CTest() {
  23. cout << "Destroy " << x << ' ' << z << endl;;
  24. };
  25.  
  26. CTest & operator=(const CTest & src) {
  27. cout << "Assign " << x << " (" << this->z << " <- " << src.z << ")" << endl;
  28. this->x = src.x;
  29. return *this;
  30. };
  31. };
  32.  
  33. CTest g(10);
  34.  
  35. const CTest & getExists() {
  36. cout << "Call getExists" << endl;
  37. return g;
  38. }
  39.  
  40. CTest createNew() {
  41. cout << "Call createNew" << endl;
  42. CTest t(20);
  43. return t;
  44. }
  45.  
  46. void test(const CTest & t) {
  47. cout << "Test " << t.x << ' ' << t.z << endl;
  48. }
  49.  
  50. int main() {
  51. cout << "======== ? ========\n";
  52. int c = 1;
  53. test(c ? getExists() : createNew());
  54.  
  55. cout << "====== getExists ========\n";
  56. test(getExists());
  57.  
  58. cout << "======= getExistsVar =========\n";
  59. const CTest & v = getExists();
  60. cout << "Call test()\n";
  61. test(v);
  62. return 0;
  63. }
Success #stdin #stdout 0s 5656KB
stdin
Standard input is empty
stdout
Create 10 1
======== ? ========
Call getExists
Copy 10 2 <- 1
Test 10 2
Destroy 10 2
====== getExists ========
Call getExists
Test 10 1
======= getExistsVar =========
Call getExists
Call test()
Test 10 1
Destroy 10 1