Rules for C + + operator overloading

Source: Internet
Author: User

The rules for operator overloading are as follows:

1. The operators in C + + are all overloaded except for a few, and can only overload the operators already in C + +.

2. The precedence and binding of operators after overloading will not change

3, operator overloading is the actual need for the new type of data, the original operators to appropriate transformation, generally speaking, the function of overloading should be similar to the original function, can not change the number of operands of the original operator, at least one Operation object is a custom type.

Note: There are only 5 operators that cannot be overloaded, they are the generic relational operators ".", the member pointer operator ". *", the scope resolution "::", the sizeof operator and the Tri-wood operator "?:". The preceding two operators ensure that the meaning of access member functionality in C + + is not changed. The operand of the scope resolution and the sizeof operator is of type.

4. There are two overloaded forms of the operator, overloading the member functions of the class and overloading the friend function for the class.

    • The general syntax for operator overloading for member functions of a class is:

function type operator operator (formal parameter list)

{

function body

}

    • Operator overloading is a friend function of a class that can declare a prototype of a friend function in a class, implemented outside the class, or implemented in a class body

Rules for C + + operator overloading

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.