title: How to return NULL object in C++
body: <p>I know that this might be a duplicate of: <a href="https://stackoverflow.com/questions/2639255/c-return-a-null-object-if-search-result-not-found">Return a &quot;NULL&quot; object if search result not found</a></p>
  
  <p>BUT, there's something different going on with my code because the asterisk doesn't solve my problem, which is this:</p>
  
  <pre><code>Normal Sphere::hit(Ray ray) {
     //stuff is done here
     if(something happens) {
         return NULL;
     }
     //other stuff
     return Normal(something, somethingElse);
  }
  </code></pre>
  
  <p>But I get an error referencing the <code>return NULL</code> line: <code>conversion from ‘int’ to non-scalar type ‘Normal’ requested</code></p>
  
  <p>And another error and warning that referencing the last return line: <code>warning: taking address of temporary</code> and <code>conversion from ‘Normal*’ to non-scalar type 'Normal' requested</code></p>
  
  <p>I understand why I am getting this warning, but I don't know how to fix it. How do I return a <code>Normal</code> object in the last line that persists after the function ends and how do I return a <code>NULL</code> object that first time? (If there's a term for these types of returns, please let me know so I can also read up on it more.)</p>
  
  <p>To clarify a commenter's question, I've tried these things:</p>
  
  <p>I tried doing this: <code>Normal *Sphere::hit(Ray ray)</code> in the cpp file and <code>Normal *hit( Ray ray );</code> in the header file and I get this error: <code>error: prototype for ‘Normal* Sphere::hit(Ray)’ does not match any in class 'Sphere'</code></p>
  
  <p>I also tried this: <code>Normal Sphere::*hit(Ray ray)</code> in the cpp file and <code>Normal *hit( Ray ray);</code> in the header file and I get this error for the second return statement: <code>cannot convert 'Normal*' to 'Normal Sphere::*' in return</code></p>
  
  <p><strong>Further clarification:</strong> I'm not asking about how pointers work. (That wasn't the main question.) I'm wondering about syntax regarding pointers in C++. So, given the function I've specified above, I've gleaned that I should specify a return a pointer because C++ doesn't have null objects. Got it. BUT, the problem then becomes: what should the function prototype look like? In the cpp file, I have what Bala suggested (which is what I had originally but changed it because of the following error):</p>
  
  <pre><code>Normal* Sphere::hit(Ray ray) {
     //stuff is done here
     if(something happens) {
         return NULL;
     }
     //other stuff
     return new Normal(something, somethingElse);
  }
  </code></pre>
  
  <p>In the header file, I have <code>Normal *hit(Ray ray)</code>, but I still get this message: <code>prototype for 'Normal* Sphere::hit(Ray)' does not match any in class 'Sphere'</code> At this point, it is unclear to me why it can't find that function prototype. Here is the header file:</p>
  
  <pre><code>class Sphere
  {
      public:
          Sphere();
          Vector3 center;
          float radius;
          Normal* hit(Ray ray);
  };
  </code></pre>
  
  <p>Can anyone see why it's complaining that there doesn't exist a matching prototype for <code>hit</code> in the <code>Sphere</code> class? (I might move this to a separate question...)</p>