The unary operators that can be overloaded are as follows:
- ! (logical "non")
- & (Address)
- ~ (binary anti-code)
- * (Cancel pointer reference)
- + (one Yuan Plus)
- -(unary negation)
- + + (increment)
- --(descending)
- Conversion operators
The postfix increment and decrement operators (+ + and –) are processed separately in increments and decrements, as described below.
The following rules apply to all other unary operators. To declare a unary operator function as a non-static member, you must declare it in the following form:
Ret-type operator OP ()
Where Ret-type is the return type, OP is one of the operators listed in the previous table.
To declare a unary operator function as a global function, you must declare it in the following form:
Ret-type operator OP (ARG)
Where Ret-type and op are used for member operator functions as described above, ARG is the parameter of the class type to participate in the operation.
Attention
There is no limit to the return type of a unary operator. For example, the logical "non" (!) return integer value is reasonable, but not mandatory.
Increment and decrement operator overloading
Because the increment and decrement operators each have two variables, they belong to a special category:
- Pre-increment and post-increment
- Pre-decrement and post-decrement
When writing overloaded operator functions, it is useful to implement a separate version of the prefix and suffix versions of these operators. To differentiate between the two, follow these rules: The operator is prefixed in exactly the same way as any other unary operator, and the suffix form accepts other parameters of type int.
Note
When you specify an overloaded operator for the prefix of an increment or decrement operator, the type of the other parameter must be int; Specifying any other type will produce an error.
The following example shows how to define the prefix and suffix increment and decrement operators for the point class:
Increment_and_decrement1.cppclass point{public://Declare prefix and postfix increment operators. point& operator++ (); Prefix increment operator. Point operator++ (int); Postfix increment operator. Declare prefix and postfix decrement operators. point& operator--(); Prefix decrement operator. Point operator--(int); Postfix decrement operator. Define default constructor. Point () {_x = _y = 0;} Define accessor functions. int x () {return _x;} int Y () {return _y;} Private:int _x, _y;};/ /Define prefix increment operator. point& point::operator++ () {_x++; _y++; return *this;} Define postfix increment operator. Point point::operator++ (int.) {Point temp = *this; ++*this; return temp;} Define prefix decrement operator. point& point::operator--() {_x--; _y--; return *this;} Define postfix decrement operator. Point point::operator--(int.) {Point temp = *this; --*this; return temp;} int main () {}
You can use the following function headers to define the same operator in a file scope (global):
Friend point& operator++ (point&) //Prefix incrementfriend point& operator++ (point&, int)//POSTF IX incrementfriend point& operator--(point&) //Prefix decrementfriend point& operator--(Point&, I NT)//Postfix Decrement
An argument of type int that represents the suffix of an increment or decrement operator is not commonly used for passing parameters. It usually contains a value of 0. However, you can use it in the following ways:
Increment_and_decrement2.cppclass int{public: int &operator++ (int n);p rivate: int _i;};i nt& int::operator++ (Int N) { if (n! = 0) //Handle case where a argument is passed. _i + = n; else _i++; Handle case where No. argument is passed. return *this;} int main () { int i; i.operator++ (25); Increment by 25.}
In addition to explicit calls, there is no syntax for passing these values using the increment or decrement operators, as shown in the preceding code. A more straightforward way to implement this functionality is to overload the addition/assignment operator (+ =).