C ++ operator overload rules

Source: Internet
Author: User

The rules for overloading operators are as follows:

1. Except a few operators in C ++, they can all be reloaded, and only existing operators in C ++ can be reloaded.

2. After overloading, the operator's priority and associativity will not change.

3. Operator Overloading is an actual need for new types of data. The original operators should be transformed as appropriate. In general, the overloaded functions should be similar to the original functions, the number of operation objects of the original operator cannot be changed. At least one operation object must be of the custom type.

Note: There are only five operators that cannot be overloaded. They are class Relational operators ". ", member pointer operator". * ", scope identifier": ", sizeof operator, and three-wood operator"? :". The preceding two operators ensure that the meaning of the access member function in C ++ is not changed. The operands of the range delimiters and sizeof operators are of type.

4. There are two methods to overload operators: Reload is a member function of the class and overload is a friend function of the class.

  • The general syntax format of a member function whose operator is overloaded as a class is:

Operator of function type (parameter table)

{

Function body

}

  • Operator Overloading is a class-based friend function. You can declare the prototype of the friend function in the class, implement it outside the class, or implement it in the class body.

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.