Unary Operators Overload (Programming)

Unary Operators Overload (Programming) . The unary operators susceptible to overloading are:

  • Unary + and – operators
  • Unary increment ++ and decrement operators –
  • Pointer operators: & reference and indirection *
  • One’s complement bitwise handling operator~
  • Logical negation operator!
  • Dynamic memory allocation and deallocation: newand delete

Summary

[ hide ]

  • 1 ++ and – operator overload
    • 1 Preincrement operator overload ++ X
    • 2 Overload of the predecrement operator – @
    • 3 Overloading the X ++ X– post-operators
  • 2 Indirection operator overload
  • 3 Overloading the logical negation operator
  • 4 You can consult
  • 5 Source

++ and – operator overload

The increment ++ and decrement– operators are overloaded differently depending on whether they are the “pre” or “post” operators. That is, assuming that @ represents one of them, the overload is different depending on the expression @ xox @. In the case of ++ / – preoperators, the overloading for the members of a C class can be done in two ways a- By declaring a non-static member-function, which does not accept arguments of the type:

C & C :: operator ++ ();

b- By declaring a non-member function (generally friend) that accepts an argument.

C & operator ++ (C & c);

Based on the above, and depending on the declaration, the expression @x can be interpreted as either of the two ways:

a- x.operator @ ()

b- operator @ (x)

If both forms have been declared, standard argument congruence is applied to resolve any possible ambiguity.

Preincrement operator overload ++ X

The technique to follow and the problems derived from overloading unary operators are shown in the following example

int x = 5, y // L.1:

y = ++ x; // L.2:

cout << “x ==” << x << “; y ==” << y << endl;

Departure:

x == 6; y == 6

In L.1 two int variables are declared and one of them is initialized, the other contains garbage. The L.2 sentence is executed from right to left: The value 5 of x is increased, becoming 6 The garbage value of y is replaced by the value 6 of x.

Pre-increment operator overload – @

#include <iostream>

using namespace std;

class Integer {

public: int x;

friend Integer & operator ++ (Integer &);

friend Integer & operator – (Integer &);

};

Integer & operator ++ (Integer & e) {

ex = ex + ex;

return e;

}

Integer & operator– (Integer & e) {

ex = ex / 2;

return e;

}

void main () {// ==============

Integer e1, e2, e3;

e1.x = 5;

e3 = ++ e2 = ++ e1;

cout << “e1 =” << e1.x << “; e2 =” << e2.x

 

e3 = –e2 = –e1;

cout << “e1 =” << e1.x << “; e2 =” << e2.x

}

Departure:

e1 = 10; e2 = 10; e3 = 10

e1 = 5; e2 = 5; e3 = 5

Post-operator overload X ++ X–

The increment ++ and decrement postoperators – can only be overloaded by defining the operator-functions in two ways: 1- By declaring a non-static member function that accepts an integer as an argument. Example:

CC :: operator ++ (int);

2- By declaring a non-member function (generally friend) that accepts an object of the class and an integer as arguments (in this order). Example:

C operator– (C &, int);

Depending on the declaration, if @ represents a unitary post-operator (++ or -), the expression x @ can be interpreted as either of two forms:

1- x.operator @ (int)

2- operator @ (x, int)

Indirection operator overload

The indirection operator * is a unary preoperator, so its overload can be done in any of the ways a or b

Logical negation operator overload

The logical negation NOT operator (!) Is related to its opposite (which has no name or representation). For example, the expression:

if (! c) {/ * … * /}

Throws a compilation error: ‘operator!’ not implemented in type ‘C’ …, indicating that the logical negation NOT operator is not defined for objects of the class. However, we can see that an analogous attempt without the operator:

if (c) {/ * … * /}

It also produces an error, although in this case the indication is more ambiguous: Illegal structure operation in function … (Borland) or: conditional expression of type ‘class C’ is illegal (Visual C ++). In the latter case, the compiler is indicating that it does not know how to convert the expression in parentheses (c) to a bool type. The if (<condition>) … statement expects to receive a <condition> expression that resolves to a bool Both problems can be resolved by adopting the appropriate measures. The first one by overloading the NOT operator for objects of the class. The second by providing a user conversion that allows the compiler to transform a type C into a bool

 

by Abdullah Sam
I’m a teacher, researcher and writer. I write about study subjects to improve the learning of college and university students. I write top Quality study notes Mostly, Tech, Games, Education, And Solutions/Tips and Tricks. I am a person who helps students to acquire knowledge, competence or virtue.

Leave a Comment