fork(1) download
  1. #include<iostream>
  2.  
  3. class myClass
  4. {
  5. private:
  6. int val;
  7. public:
  8. myClass () = delete;
  9. myClass (int val):val
  10. {
  11. val}
  12. {
  13. }
  14. int get () const
  15. {
  16. return val;
  17. }
  18. };
  19.  
  20. bool
  21. ifEqualMake (int a, int b, myClass * obj)
  22. {
  23. if (a == b)
  24. {
  25. obj = new myClass (a);
  26.  
  27. }
  28. else
  29. {
  30. std::cout << "Difference exists: " << a - b << '\n';
  31. // Uncommenting the below line makes the whole thing work!
  32. obj = new myClass (a + b);
  33. }
  34. std::cout << " Object made with value :" << obj->get () << '\n';
  35. return (a == b);
  36. }
  37.  
  38. int
  39. main ()
  40. {
  41. myClass *obj1 = nullptr;
  42. myClass *obj2 = nullptr;
  43. myClass *obj3 = nullptr;
  44.  
  45.  
  46. ifEqualMake (3, 3, obj1);
  47. ifEqualMake (4, 3, obj2);
  48. ifEqualMake (4, 4, obj3);
  49.  
  50. if(obj1) std::cout << "obj 1 made in heap: " << obj1->get () << '\n';
  51.  
  52. // I would expect ONLY this to be error
  53. // std::cout<<"obj 2 made in heap: "<<obj2->get()<<'\n';
  54. //
  55. if(obj3) std::cout << "obj 3 made in heap: " << obj3->get () << '\n';
  56.  
  57. delete obj1;
  58. delete obj3;
  59.  
  60. return 0;
  61.  
  62. }
  63.  
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
 Object made with value :3
Difference exists: 1
 Object made with value :7
 Object made with value :4