How to return NULL object in C++

2020-07-02 10:10发布

I know that this might be a duplicate of: Return a "NULL" object if search result not found

BUT, there's something different going on with my code because the asterisk doesn't solve my problem, which is this:

Normal Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return NULL;
   }
   //other stuff
   return Normal(something, somethingElse);
}

But I get an error referencing the return NULL line: conversion from ‘int’ to non-scalar type ‘Normal’ requested

And another error and warning that referencing the last return line: warning: taking address of temporary and conversion from ‘Normal*’ to non-scalar type 'Normal' requested

I understand why I am getting this warning, but I don't know how to fix it. How do I return a Normal object in the last line that persists after the function ends and how do I return a NULL 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.)

To clarify a commenter's question, I've tried these things:

I tried doing this: Normal *Sphere::hit(Ray ray) in the cpp file and Normal *hit( Ray ray ); in the header file and I get this error: error: prototype for ‘Normal* Sphere::hit(Ray)’ does not match any in class 'Sphere'

I also tried this: Normal Sphere::*hit(Ray ray) in the cpp file and Normal *hit( Ray ray); in the header file and I get this error for the second return statement: cannot convert 'Normal*' to 'Normal Sphere::*' in return

Further clarification: 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):

Normal* Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return NULL;
   }
   //other stuff
   return new Normal(something, somethingElse);
}

In the header file, I have Normal *hit(Ray ray), but I still get this message: prototype for 'Normal* Sphere::hit(Ray)' does not match any in class 'Sphere' At this point, it is unclear to me why it can't find that function prototype. Here is the header file:

class Sphere
{
    public:
        Sphere();
        Vector3 center;
        float radius;
        Normal* hit(Ray ray);
};

Can anyone see why it's complaining that there doesn't exist a matching prototype for hit in the Sphere class? (I might move this to a separate question...)

标签: c++
7条回答
时光不老,我们不散
2楼-- · 2020-07-02 10:58

The NULL return value would only be valid if you were returning a pointer to a Normal object, NULL represents a null pointer, not a null object.

What I would do in this case is define a 'null' or invalid state for this object. Since you are working with surface normals, you can consider a normal with length == 0 an invalid state, so then you would do this:

Normal Sphere::hit(Ray ray) {
   //stuff is done here
   if(something happens) {
       return Normal();
   }
   //other stuff
   return Normal(something, somethingElse);
}

Then your normal class would have something like this:

class Normal {
public:
    Normal() : x(0), y(0), z(0), len(0) {}
    // ... other constructors here ...

    bool isValid() const { return (len != 0) };

    // ... other methods here ...

private:
    float x, y, z, len;
};
查看更多
登录 后发表回答