title: return an pointer to an object from a function without using new to allocate the pointer
body: <p>From the thread in </p>
  
  <p><b> <a href="https://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c">When should I use the new keyword in C++?</a> </b></p>
  
  <p>The answer talks about when "new" must be used in order to create a pointer to an object if you need to return the pointer to the object from the function.</p>
  
  <p>However, my code below works fine. I use a local pointer instead of allocating some memory to a new pointer.</p>
  
  <pre><code>node* queue::dequeue(){
    if(head==0){
      cout&lt;&lt;"error: the queue is empty, can't dequeue.\n";
      return 0;
    }
    else if(head-&gt;next !=0){
      node *tmp=head;
      head=head-&gt;next;
      tmp-&gt;next=0;
      return tmp;
    }
    else if(head-&gt;next ==0){
      node *tmp=head;
      head=0;
      tmp-&gt;next=0;
      return tmp;
    }
  }
  </code></pre>
  
  <p>It is a simple dequeue() operation. My tmp is a local pointer. But i still return it.</p>
  
  <p><strong>Credit to Mahesh</strong></p>
  
  <p>I have following statements in the main()</p>
  
  <pre><code>node a8(8); //node constructor with the value
  </code></pre>
  
  <p>Therefore tmp points to what head points to, and head points to different nodes like a8.</p>
  
  <p>Since a8 is valid throughout the main(), tmp is valid throughout the main() as well </p>