Home > other >  Why we must overloading = and -= beside just overloading and - operator?
Why we must overloading = and -= beside just overloading and - operator?

Time:04-25

In c , why we must overloading =, -=, , - operator beside just overloading and - operator? Here is an example:

In C , when I create a Point class, I will do:

class Point {
public:
    int x, y;
public:
    Point(int X, int Y) : x(X), y(Y) {}
    //Assignment operator
    void operator=(Point a) { x = a.x; y = a.y; }
    //The  = and -= operator, this seem problematic for me. 
    void operator =(Point a) { x  = a.x; y  = a.y; }
    void operator-=(Point a) { x -= a.x; y -= a.y; }
    //The   and - operator
    Point operator (Point a) { return Point(x   a.x, y   a.y); }
    Point operator-(Point a) { return Point(x - a.x, y - a.y); }
};

But in some other language like C# for example, we don't need to overloading the = and -= operator:

public class Point {
    public int x, y;
    public Point(int X, int Y) {
        x = X; y = Y;
    }
    //We don't need to overloading =,  = and -= operator in C#, all I need to do is overload   and - operator
    public static Point operator (Point a, Point b) { return Point(a.x   b.x, a.y   b.y); }
    public static Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); }
}

And both will work same as c !

So I already know that if we overloading like c , we can easier control which operator this class can have. But what else it can do?

I'm also new in c and I just learn overloading operator today.

CodePudding user response:

The typical operator = is more efficient than a = a b, and cannot be implemented in terms of operator . It can be the other way around though:

struct foo {
    int value = 42;
    foo& operator =(const foo& other) {
         this.value  = other.value;
         return *this;
    }
    foo operator (const foo& other) const {
         foo result = *this;
         result  = other;   // reuse operator =
         return result;
    }
};

Note how operator must create a new instance, while operator = merely adds member of already existing instances.

In general operator = can do something else entirely and might not be related to operator at all.

The same holds for operator-= vs operator-.

CodePudding user response:

The questions that should be asked are rather:

  1. Why C# wont allow overloading assignment operator?
  2. Why C can't automatically do = if you have overloaded ' ' and '=' operators?

The first question is discussed here with an excellent answer: https://stackoverflow.com/a/599582/6035486 TDLR; Due to garbage collection and reference counting, the compiler must be sure that the left side operand of an assignment statement (which is a reference because in C# class instances are references) is invalidated during the assignment hence its reference count must decrease by one. If user-defined assignment logic is allowed and anything can happen in an assignment operator, this mechanism of garbage collection would not work. Sorry this was supposed to be TLDR.

The answer to the second question is the same reason why defining operator== won't automatically give you operator!= and it should also be defined separately.

  • Related