answersLogoWhite

0


Best Answer

The right-hand operand; the r-value of the operator.

Unary operators have one operand while tertiary operators have three operands. All binary operators have two operands, the l-value and the r-value. The l-value is the operand to the left of the operator while the r-value is the operand to the right of the operator.

Thus, in the expression x + y, x is the l-value while y is the r-value.

When overloading binary operators in a class, you need only specify the r-value. The l-value is the instance of the class to which the operator applies and therefore does not need to be specified. For instance:

class MyClass

{

public:

MyClass(int data=0):m_data(data){} // default constructor

int operator+ (const int rhs) const {return(m_data+rhs);}

private:

int m_data;

};

While this allows you to return the sum of your class instance and an integer, it does not allow you to return the sum of an integer and an instance of your class. For example:

MyClass obj(5);

int x = 10;

int y = obj + x; // OK! y is 15.

int z = x + obj; // Compiler error! No operator exists that accepts an r-value of type MyClass.

To fix this error and allow for two-way addition, you must declare a binary operator overload outside of the class. You cannot do it inside the class because the l-value is an int, not an instance of MyClass.

The external overload requires two parameters, the l-value and the r-value of the operator:

int operator+(const int lhs,const MyClass& rhs) {return(rhs+lhs);}

Note that the implementation simply reverses the operands. This is functionally equivalent to making the following explicit call:

return(rhs.operator+(lhs));

Note also that since MyClass::operator+ is a public operator of MyClass, this overload does not need to be declared a friend of MyClass (a common misconception). However, the overload must be declared in the same file where the class is declared since it is only of relevance to MyClass but should be made available wherever MyClass is accessible.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which operand should be passed in the binary overloaded operator function as a second operand?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is operand in c plus plus?

An operand is the value that is being operated upon by an operator. For instance, the C++ increment operator (++) is a unary operator, which means it has only one operand, the variable that we wish to increment. This in the expression x++, x is the operand. The addition operator (+) is a binary operator and therefore has two operands. Thus in the expression x + y, x and y are the operands.


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


What is unary minus operator?

No. The subtraction operator is a binary operator that returns the result of subtracting the rhs operand from the lhs operand. The unary minus operator simply negates the rhs operand. int x = -5; // unary minus. x is (-5) int y = -x; // unary minus. y is (+5) y -= x; // binary minus/assign operator. y is (+10) --x; // unary decrement operator. x is (-6) y -= (-x); // binary minus/assign and unary minus operators. y is(+4)


What is modulus function?

It is an binary arithmetic operator which returns the remainder of division operation. It can used in both floating-point values and integer values. opLeft % opRight where, opLeft is the left operand and opRight is the right operand. This expression is equivalent to the expression opLeft - ((int) (opLeft / opRight) * opRight)


What is difference between conditional operator and bitwise operator?

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.

Related questions

What is operand in c plus plus?

An operand is the value that is being operated upon by an operator. For instance, the C++ increment operator (++) is a unary operator, which means it has only one operand, the variable that we wish to increment. This in the expression x++, x is the operand. The addition operator (+) is a binary operator and therefore has two operands. Thus in the expression x + y, x and y are the operands.


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


What is unary minus operator?

No. The subtraction operator is a binary operator that returns the result of subtracting the rhs operand from the lhs operand. The unary minus operator simply negates the rhs operand. int x = -5; // unary minus. x is (-5) int y = -x; // unary minus. y is (+5) y -= x; // binary minus/assign operator. y is (+10) --x; // unary decrement operator. x is (-6) y -= (-x); // binary minus/assign and unary minus operators. y is(+4)


Why ostream operators not overloaded using member functions?

Consider the following line: cout<<obj; where obj is the object of Demo class. In this case we are overloading "<<" operator. But overloading the binary operator using member function, the left hand operand should be the object of relevant class. Here in this case left hand side operand is not the object of Demo class. It is object of ostream class. Hence we cant overload ostream operators using member function. But we can overload these type of operators using friend functions. Thanks, Prof. D. H. Ingole


What is modulus function?

It is an binary arithmetic operator which returns the remainder of division operation. It can used in both floating-point values and integer values. opLeft % opRight where, opLeft is the left operand and opRight is the right operand. This expression is equivalent to the expression opLeft - ((int) (opLeft / opRight) * opRight)


What is difference between conditional operator and bitwise operator?

A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.A binary operator is simply an operator that has two parts, written to the left and to the right of the operator, e.g.:1 + 2The binary operator can be a logical operator ("and", "or", "xor", etc. - but "not" is a unary operator), or it can be in some other category, like the arithmetic operator shown above.


What represents the left hand side of a binary operator in c plus plus?

The left hand operand of a binary operator must represent a modifiable lvalue: void f (const int x, int y) { x = y; // error -- x is constant lvalue y = x; // ok }


What is the difference between the assignment operator and the equals operator?

The quality operator and the assignment operator are binary operators; they have two operands, one on either side of the operator. The equality operator is a Boolean operator which compares the two operands, returning true if they have the same logical state, otherwise false. E.g., x==y returns true if x and y have the same logical state, otherwise false. The operator is commutative, such that x==y is the same as y==x. The assignment operator sets the value of the left operand to that of the right operand, such that they both have the same logical state. After assignment, the left operand is returned. E.g., x=y returns x while y=x returns y. After the assignment, x==y must be true.


What is binary operation?

A binary operator is simply an operator that works with two operands (for example, two numbers). The binary operator is usually written between the two operands. Examples include the familiar operations of addition, subtraction, multiplication, or division - for example, in: 2 + 3 the "plus" is the binary operator, which works on the two numbers written on either side of it. What is an operator: Basically a function (calculation rule), written in a special way.


What does the modulus operator in Java do?

It is an binary arithmetic operator which returns the remainder of division operation. It can used in both floating-point values and integer values. opLeft % opRight where, opLeft is the left operand and opRight is the right operand. This expression is equivalent to the expression opLeft - ((int) (opLeft / opRight) * opRight)


What is a binary operation?

A binary operator is simply an operator that works with two operands (for example, two numbers). The binary operator is usually written between the two operands. Examples include the familiar operations of addition, subtraction, multiplication, or division - for example, in: 2 + 3 the "plus" is the binary operator, which works on the two numbers written on either side of it. What is an operator: Basically a function (calculation rule), written in a special way.


When a symbol such as is placed between 2 expressions?

That's called an "operator", for example, the plus sign in 3 + 4. The numbers are the "operands" - that is, the numbers (or expressions) operated upon. More precisely, a binary operator, since it works with two operands. In theory, an operand is just a special way of writing a function, since a third number is calculated, according to certain rules, from the other two.