C ++ 10th-Operator Overloading

Source: Internet
Author: User

1. Introduction to Operator Overloading

The previous function overload allows multiple functions of different parameter types with the same name;

Operator Overloading allows programmers to use operators (such as +,-, =, =, And !) To process different types of data;

Operators can be used for basic data types:


For custom data types:


If you use the custom mystring operator + definition, a compilation error occurs.

When an operator is overloaded, C ++ selects an appropriate overload Operator Based on the Data Type;

Except for arithmetic if (? :), Sizeof, scope (: :), member selector (.), and member pointer selector (. *) cannot be overloaded;

Before entering the topic, you must be clear:

A) At least one of the overloaded operation operations is custom data;

B) only existing operators can be reloaded;

C) all operators maintain their priority and combination;

2) operators are functions.

In C ++, operators are treated as operators, such as <,>.

2. Overload Arithmetic Operators

Arithmetic Operators: +,-, *,/, and %

These four operators are binary, And the reload method is almost the same.

1) Use the friend functions to overload Operators

When the operator does not change the operation operand, it is best to use the youyuan function to overload the operator.


2) Reload operators for different types of operands

When the binary operators are overloaded for different types of operands, two functions are required. When the + parameter in the example above is a cent and an integer:


3. overload I/O Operators

1) heavy load output OPERATOR: <


2) overload input character:>


Reload the I/O operator to output your custom class;

4. Reload the comparison operator

Comparison operators: = ,! =,>,> =, <, <=

Because the comparison operators are binary operators and do not need to change the parameter values, you can use the youyuan function to overload them;

Still use coins:



5. Overload cell Operators

Unit operators: +,-, and ,-,!


6. Use the member function overload Operator

You can use this function to reload data without modifying the parameter values of the operation;

When you need to modify the operand, use the member function overload operator;

Note two points: a) the leftmost operand must be a class object; B) the leftmost operand is similar to the * this parameter.

Two exceptions:

A) The leftmost parameter is not a member of the class, such as overload + (INT, yourclass) or <(ostream & out, yourclass). It must be overloaded as a friend;

B) Assignment =, brackets [], call () and member selection->, must be used as a member function overload.

2) overload Unit operators + ,-


A typical application of member functions is to modify private member variables.

7. Reload the auto increment and auto increment Operators

The auto-increment (++) and auto-increment (--) operators can be prefixed or suffixed.

Because the auto-increment and auto-increment functions modify the operands, it is best to reload the member functions.

1) prefix:

\

2) Suffix:

When the suffix is overloaded, C ++ uses a dummy variable, which is false:



Because of the suffix method, a value must be assigned to return the status before auto-increment and auto-increment;

8. Reload the brackets Operator

Brackets are one of the operators that must be overloaded by member functions.

It is usually used in arrays.


Brackets are generally useful when processing one-dimensional arrays.

9. Reload the bracket Operator

For example:


Of course, the overload () operator can also be left with no parameters.

10. Heavy Load type conversion character

Previously, explicit and implicit conversions were available for type conversion;


You can also perform other useful conversions:


11. copy constructors and overload assignment operators

1) copy the constructor

The copy constructor initializes a new object from an existing object;


12. Shallow copy and deep copy

Shallow copy-only copies the object values to other objects. modifies other objects without changing the original objects;

Deep copy-returns the reference pointer of an object to other objects. modifies other objects and the original objects.

To prevent copying: You can privatize the constructor with parameters and the overload assignment operator.

[Disclaimer:
1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog !]

Related Article

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.