Tom Dara
Tom Dara

Reputation: 61

Overloading the `=` operator in C++

I am trying to understand overloading operators in c++. I've written some code below trying to overload the = operator. However, it doesn't work correctly and I am wondering why now.

I've followed the example that is provided here for the + operator.

Operators Overloading in C++

#include <iostream>
using namespace std;

class Test {
private:
    int id;
    string name;

public:
    Test(): id(0), name(""){

    }

    Test(int id, string name): id(id), name(name) {

    }

    void print() {
        cout << id << ":" << name << endl;

    }
    Test operator=( Test &other) {

        Test test;
        cout << "Assignment running" << endl;

        test.id =other.id;
        test.name =other.name;

        return test;

    }
};

int main() {

    Test test1(10, "Mike");
    cout << "Print Test1" << endl;
    test1.print();

    Test test2;

    test2.operator=(test1);
    cout << "Print Test2" << endl;
    test2.print();


    cout << endl;

}

Upvotes: 0

Views: 96

Answers (3)

Steve
Steve

Reputation: 1757

Because unlike +, = needs to modify the object in question, rather than a temporary variable that you then return.

Test& operator=(const Test &other) {

    cout << "Assignment running" << endl;

    id =other.id;
    name =other.name;

    return *this;
}

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234655

operator=, by standard, should modify this, and return a reference to *this (in order to allow for compound assignments):

Test& operator=(const Test& other) {
    cout << "Assignment running" << endl;

    this->id = other.id;
    this->name = other.name;

    return *this;
}

Note that I'm also passing other as const. Your version does not modify this, which will have surprising effects.

Upvotes: 4

Useless
Useless

Reputation: 67713

Start by thinking about what semantics you expect. Since the whole point of overloading operators is that you can use the same syntax as built-in operators, replace

test2.operator=(test1);

with

test2 = test1;
  • First, what do you expect to happen to test1 in this expression? Should it change?

    Hint: the answer is no, so the right-hand-side parameter should be passed by const reference.

  • Secondly, what do you expect to happen to test2 in this expression? Should it change?

    Yes, of course it should change, you're assigning to it. So, the left-hand-side argument should be modified by your code. Note that in a member operator, the left-hand side is this.

So, we know we should at least change your code to take the right-hand-side parameter by const reference, and that it ought to modify this rather than leaving it untouched while assigning to a temporary.

Further, the canonical forms of all these operators are well-known and documented. For example, here

Test& operator=(const Test &other) {
    cout << "Assignment running" << endl;

    this->id =other.id;
    this->name =other.name;

    return *this;
}

Upvotes: 1

Related Questions