An explanation of overloading of unary operators in C + + programming

Source: Internet
Author: User
The unary operators that can be overloaded are as follows:

    1. ! (logical "non")
    2. & (Address)
    3. ~ (binary anti-code)
    4. * (Cancel pointer reference)
    5. + (one Yuan Plus)
    6. -(unary negation)
    7. + + (increment)
    8. --(descending)
    9. 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 (+ =).

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.