0

I've made a very simple program trying to understand operator overloading in C++. However as you too will see, the result of the dimention d3 is not updated even though the appropriate values are returned from the operator overloading.

#include <iostream>

using namespace std;

class dimention{
    
protected:
    int width, height;
    
public:
    
    dimention(int w = 0, int h = 0){
        width = w;
        height = h;
    }
    
    int getWidth(){
        return width;
    }
    int getHeight(){
        return height;
    }

    dimention& operator = (const dimention &d){

        dimention *temp = new dimention;
        temp->height = d.height;
        temp->width = d.width;

        return *temp;
    }
    
    dimention& operator + (const dimention &d){
        
        dimention *newDimention = new dimention;
        
        newDimention->width = this->getWidth() + d.width;
        newDimention->height = this->getHeight() + d.height;

        return *newDimention;
    }
    
};

int main(){
    
    dimention *d1 = new dimention(5, 5);
    dimention *d2 = new dimention(1, 1);
    dimention *d3 = new dimention;

    *d3 = *d1;
    cout << d3->getHeight() << endl;
    cout << d3->getWidth() << endl;

    *d3 = *d1 + *d2;

    cout << d3->getHeight() << endl;
    cout << d3->getWidth() << endl;

    return 0;
}

Thanks for your help.

  • Unrelated tactical note: You don't have to `new` everything in C++, and there are performance and mental strain reasons to `new` as little as possible. – user4581301 May 24 '22 at 21:40
  • 3
    `operator =` should update the contents of `this`, no? In other words, it doesn't need to allocate a new object, but rather, just to update the fields of `this`, and then return `*this`. –  May 24 '22 at 21:40
  • 3
    Handy reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 May 24 '22 at 21:47
  • 1
    Read up on the rule of zero: https://en.cppreference.com/w/cpp/language/rule_of_three – Goswin von Brederlow May 24 '22 at 21:48

1 Answers1

0

I think you misunderstand the way methods operate on an object.

Consider the assignment operator:

dimention& operator = (const dimention &d){

    dimention *temp = new dimention;
    temp->height = d.height;
    temp->width = d.width;

    return *temp;
}

You are never editing the object itself (this) that is being assigned to. Instead, you are creating (and leaking) a new temp object and changing it. That object is not d3.

A correct implementation would be:

dimention& operator = (const dimention &d){
    this->height = d.height;
    this->width = d.width;
    return *this;
}

Will give you the expected result.

Daniel Trugman
  • 8,186
  • 20
  • 41
  • `operator+` needs to return a new (but not `new`'ed) `dimention` object, not modify `this` at all, eg: `dimention operator + (const dimention &d) const { dimention newDimention; newDimention.width = width + d.width; newDimention.height = height + d.height; return newDimention; }` Modifying `this` is a job for `operator+=` instead, eg: `dimention& operator += (const dimention &d) { width += d.width; height += d.height; return *this; }` – Remy Lebeau May 25 '22 at 00:10
  • @RemyLebeau, you are right. I removed that part. – Daniel Trugman May 26 '22 at 07:56