Why is the scope of object created using new limited? : <p>Object made using <code>new</code> operator does not seem to be available outside the scope! Isn't that the whole point of the <code>new</code> operator?</p>

<p><a href="https://ideone.com/DDvo9y" rel="nofollow noreferrer">https://ideone.com/DDvo9y</a> - Please check this link to see the result. </p>

<pre><code>#include&lt;iostream&gt;

class myClass
{
private:
  int val;
public:
    myClass () = delete;
    myClass (int val):val{val}{}
    int get () const
    {
      return val;
    }
};

bool ifEqualMake (int a, int b, myClass * obj)
{
  if (a == b)      obj = new myClass (a);
  else{
      std::cout &lt;&lt; "Difference exists: " &lt;&lt; a - b &lt;&lt; '\n';
      obj = new myClass (a + b);
    }
  std::cout &lt;&lt; " Object made with value :" &lt;&lt; obj-&gt;get () &lt;&lt; '\n';
  return (a == b);
}

int main ()
{
  myClass *obj1 = nullptr;
  myClass *obj2 = nullptr;
  myClass *obj3 = nullptr;

  ifEqualMake (3, 3, obj1);
  ifEqualMake (4, 3, obj2);
  ifEqualMake (4, 4, obj3);

  if(obj1) std::cout &lt;&lt; "obj 1 made in heap: " &lt;&lt; obj1-&gt;get () &lt;&lt; '\n';
  if(obj2) std::cout &lt;&lt; "obj 2 made in heap: " &lt;&lt; obj2-&gt;get()&lt;&lt;'\n';
  if(obj3) std::cout &lt;&lt; "obj 3 made in heap: " &lt;&lt; obj3-&gt;get () &lt;&lt; '\n';

  delete obj1;
  delete obj2;
  delete obj3;

  return 0;

}

</code></pre>
