Search code examples
c++overloading

How does rhs work?


Here I understand rhs means right hand side but I don't understand how does compiler understand "rhs" refers to right hand side. And can someone explain in which case will this overloading be necessary?

MyArray<T>& operator=(const MyArray<T>& rhs); 

Solution

  • The compiler doesn't know that rhs stands for "right hand side", and in fact the name of that variable can be anything you like.

    The compiler "knows" how to format this because the syntax of operator= requires it to be this way.

    class A
    {
    public:
       A& operator=(const A& other);
    };
    

    The language defines the usage of this operator to take the form:

    A a, b;
    a = b;
    

    The code above calls A::operator=(const &other) against the instance of A named a, and uses the instance of A named b as other.