Detailed explanation of the overload of the Meta operator in C ++ programming, detailed explanation of the overload of the c Operator

Source: Internet
Author: User

Detailed explanation of the overload of the Meta operator in C ++ programming, detailed explanation of the overload of the c Operator

The reloaded unary operators are as follows:

  1. ! (Logical "not ")
  2. & (ACCESS)
  3. ~ (Binary anticode)
  4. * (Cancel Pointer Reference)
  5. + (Mona1 plus)
  6. -(One-dollar reverse search)
  7. ++ (Incremental)
  8. -- (Decrease)
  9. Conversion Operators

The postfix increment and Decrement Operators (++ and --) are processed separately in increment and decrement.

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 ()
Ret-type is the return type, and op is one of the operators listed in the table above.
To declare a unary operator function as a global function, you must declare it in the following form:
Ret-type operator op (arg)
Ret-type and op are used for member operator functions as described above. arg is a parameter of the class type to be involved in calculation.
Note:
The return type of The unary operator is not limited. For example, the logical "not "(!) The return integer is reasonable, but not mandatory.

The increment and decrement operators are overloaded.
Since the increment and decrement operators have two variables, they belong to a special category:

  • Pre-increment and post-Increment
  • Pre-decrease and post-Decrease

When writing overloaded operator functions, it is useful to implement separate versions for the prefixes and suffix versions of these operators. To distinguish between the two, follow the rules below: The prefix format of the operator is exactly the same as the method for declaring any other unary operator; the suffix format accepts other parameters of the int type.

Note:
When you specify the overload operator as the prefix of the increment or decrement operator, the type of other parameters must be int. If you specify any other type, an error is returned.
The following example shows how to define the prefix and suffix increment and decrease 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 header to define the same operator in the file range (globally:

friend Point& operator++( Point& )   // Prefix incrementfriend Point& operator++( Point&, int ) // Postfix incrementfriend Point& operator--( Point& )   // Prefix decrementfriend Point& operator--( Point&, int ) // Postfix decrement

Int-type parameters with the suffix of the increment or decrease operator are not often used to pass parameters. It usually contains a value of 0. However, you can use it as follows:

// increment_and_decrement2.cppclass Int{public:  Int &operator++( int n );private:  int _i;};Int& Int::operator++( int n ){  if( n != 0 )  // Handle case where an 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 decrease operator, as shown in the previous code. The more direct method to implement this function is to overload the addition/value assignment operator (+ = ).

Articles you may be interested in:
  • Explanation of single object Operator Overloading and binary operator overloading in C ++ Programming
  • Detailed explanation of C ++ operator overload rules
  • Introduction to Operator Overloading in C ++ Programming
  • C ++ * Operator Overloading
  • Basic knowledge of c ++ Operator Overloading
  • Detailed analysis of the method for overloading the C ++ Operator
  • Detailed description of C ++ operator overload member functions and friend Functions

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.