views:

63

answers:

1

I found in some legacy code I'm dealing with this function (in C++)

Vec3d Minimum()
{
    if(this->valid)
    {
        return minBB;
    }
    else
    {
        return NULL;
    }
}

where Vec3d is a object that is basically a struct with x,y,z and some operators overloaded (code below).

AFAIK, you can't return a 0 for a user defined object... or is there some automatic cast to zero that I didn't know about? This is just out of curiosity :p

Thanks

class Vec3d
{
public:
    double x,y,z;

    /// \brief Default constructor initializes x and y to 0
    Vec3d();

    /** \brief Constructor initializies vector to input parameters x and y and z
     *
     *  \param x Double value that initializes x value of vector
     *  \param y Double value that initializes y value of vector
     *  \param z Double value that initializes z value of vector
     */
    Vec3d(double x, double y, double z);

    /** \brief Copy constructor
     *
     *  \param v Pointer to another vec3i with which to initialize current vec3i
     */
    Vec3d(Vec3d* v);

    /**\brief Sets a vector (already instantiated) to the input parameters (x,y,z)
     *
     *  \param x Double value that initializes x value of vector
     *  \param y Double value that initializes y value of vector
     *  \param z Double value that initializes z value of vector
     *
     *  This method is just so you can change the value of an already instantiated vector
     */
    void set(double xi, double yi, double zi);

    const Vec3d operator -(const Vec3d &other) const;
    const Vec3d operator +(const Vec3d &other) const;
    const Vec3d operator *(const double num) const;
    const double operator *(const Vec3d &other) const;
    const Vec3d operator /(const double num) const;
    double magnitude();
};
+5  A: 

0 can be used in the context of pointers to be the null pointer constant. That is, it's going into here:

Vec3d(Vec3d* v); 

Note the comment is incorrect, as that is not a copy-constructor.

The code is a bit shoddy. There doesn't need to be a set function, and typically non-mutating operators should be free-functions. And not least of all, having a constructor like it has is a waste and confusing. If you have a pointer to a vector, you should do:

Vec3d v = *other;

Not provide an implicit conversion from a pointer.

GMan
Oh man, I didn't see that part of the constructor haha. Thanks for point it out, I feel stupid right now :P I'll accept the answer when I can, gotta wait 3 minutes.And the code is terrible. You haven't seen half of it... I'd totally post some of this on the dailywtf, but I feel like the professor is going to catch me or something haha.
Xzhsh
never feel stupid for asking, more stupid is if one doesn't ask
Anders K.