C + + Operator overloading

Source: Internet
Author: User

I. Overloading rules

I. Operators that can be overloaded

+ - * / %
^ & | ~ !
= > < += -=
*= /= %= ^= &=
|= >> << >>= <<=
== != >= <= &&
|| ++ -- ->* ,
- [] () operator NEW Operator New[]
operator delete operator delete []

Ii. operators that cannot be overloaded

:: . .* ? :
sizeof typeID New Delete
Static_cast dynamic_cast Const_cast Reinterpret_cast

III. Basic Rules


1. The unary operator can be a member function without parameters [1] or a non-member function with one parameter [1].
2. The two-dollar operator can be a member function with one parameter [1] or a non-member function with two arguments [1].
3.operator=, operator[], operator (), operator-> can only be defined as member functions [1].
The return value of the 4.operator-> must be a pointer or an object that can be used.
5. Overloads operator++ and operator--with an int parameter representing the suffix, prefixed with no parameters.
6. In addition to operator new AND operator Delete, there must be at least one non-built-in data type in the overloaded operator parameter.
[email protected] The search scope is: X member function---Global function/x function in the namespace function/y The friend of the/x in the name space
The friend function of number/y.
8. Overloaded operators should try to simulate the behavior of the operator's built-in type.

C + + Operator overloading

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.