Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); 
like image 809
onur Avatar asked Nov 23 '16 14:11

onur


People also ask

What is the difference between RHS and SAS?

What is the difference between SAS and RHS? In SAS rule, we consider the angle between two sides that we are equal, but in RHS, the placement of angle does not matter as we take hypotenuse and any one of the other two corresponding sides.

What is RHS in math geometry?

The test is therefore given the initials RHS for 'Right angle', 'Hypotenuse, 'Side'. RHS congruence test. The hypotenuse and one side of one right-angled triangle are respectively equal to the hypotenuse and one other side of another right-angled triangle then the two triangles are congruent.

What is RHS criterion?

Ans: By RHS criteria, two right-angled triangles are said to be congruent if the hypotenuse and one side of one triangle are equal to the hypotenuse and corresponding side of the other triangle.


4 Answers

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.

like image 171
Chad Avatar answered Sep 21 '22 21:09

Chad


The standard assignment operator to an instance of the same type has the prototype

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

The name rhs is normally given to the function parameter since it appears on the right hand side of an assignment when the operator is invoked. It improves the legibility of your source code, that's all.

like image 26
Fitzwilliam Bennet-Darcy Avatar answered Sep 23 '22 21:09

Fitzwilliam Bennet-Darcy


rhs is just a name people usually use for that operator, it has no special meaning. The way that operator is defined always makes the argument the right-hand element.

like image 35
Kiskae Avatar answered Sep 21 '22 21:09

Kiskae


If you do something like:

int a = 5;
int b = 3;
a = b;

The assignment part is actually just a function call:

a.operator=(b);

Nothing special going on. The parameter name doesn't matter, just the signature which consists of the return type and parameters types, not names.

like image 29
Hatted Rooster Avatar answered Sep 20 '22 21:09

Hatted Rooster