简体   繁体   English

C ++重载运算符并返回相同的对象

[英]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 /. 我正在为Vector和Matrices编写一个类,我想了解当我想重载+,-,*和/之类的常用运算符时如何避免开销和泄漏。

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): 创建operator-作为名称空间作用域函数(而不是成员函数):

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. 测试1将答案存储在新的向量(结果)中,并假设将其返回作为对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. 测试2将答案存储在调用了函数(aVector)的向量中,并将其返回作为对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. 尽管使用测试2将节省内存,但答案存储在函数外部的两个位置(rVector和aVector),这可能不是您想要的。 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. 测试1是更安全的选择,但请确保按值返回它,以避免此处其他人指出的范围外错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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