简体   繁体   中英

C++ overloading operators and returning same object

I'm coding a class for Vector and Matrices and I'd like to understand how can I avoid overheads and leaks when I want to overload common operators such as +, -, * and /.

For example:

int main()
{
    Vector3 aVector; //This has an address #1
    Vector3 bVector; //This has another address #2

    //rVector has an address #3
    Vector3 rVector = aVector - bVector; //What will happen here?
}

And the vector class:

class Vector3
{
    public:
        float vX, vY, vZ;
        Vector3& operator-(const Vector3& vector3)
        {
            //I want to calculate this vector with the "vector3" param
            //But then what do I return?

            //Test 1:
            Vector3 result; //This has an address #4
            result.vX = vX - vector3.vX;
            result.vY = vY - vector3.vY;
            result.vZ = vZ - vector3.vZ;

            return result; //Did I just overwrite address #3?

            //Test 2:
            vX = vX - vector3.vX;
            vY = vY - vector3.vY;
            vZ = vZ - vector3.vZ;

            return (*this); //What happened to address #3? And I just changed this vector's values and I need then again later
        }
}

What's the best way to do this?

edit : One more question, if I want to do this:

Vector3 myVector = someVector - Vector3(x, y, z);

How do I code the constructor so it doesn't do anything... bad? I'm thinking it'll be creating a new class but I won't have any means to reference it after it's used in the sentence above, can this lead to problems later?

Your implementation doesn't have any leaks. Every vector you create is destructed when getting out of the scope of the function.

As for the return value, I suggest you return it by value (just remove the &).

For your second question: It's not a new so there is no leak. Also, you don't need to reference it so there is no real issue.

Create operator- as a namespace scope function (not as a member function):

Vector3 operator-(const Vector3 &lhs, const Vector3 &rhs)
{
  Vector3 result{lhs};

  // Subtract out rhs
  ...

  // Do the math
  return result;
}

Nothing gets leaked in any case, because you haven't allocated anything off of the heap (free store).

Test 1 will store the answer in a new vector (result) and hypothetically return it as a reference to rVector.

rVector = result

aVector = //its original value

Test 2 will store the answer in the vector the function was called on (aVector) and returns that as a reference to rVector.

rVector = aVector

aVector = //the answer

Although using test 2 will save memory the answer is stored in two places outside the function (rVector and aVector) which may not be what you want. Test 1 is the safer option but make sure to return it by value to avoid out-of-scope errors as others here have pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM