Simple summary of operator overloading for VC + +

Source: Internet
Author: User

Encapsulation inheritance and polymorphism are the three basic pillars of object-oriented.
There are two ways to compile in an object-oriented system: static and dynamic linking
Static linking: Also called early-linking: refers to the system at compile time to decide how to implement a certain action, providing the advantages of fast execution speed.
Dynamic linking: Also known as lag-linked: Refers to the System runtime dynamic implementation of an action, it provides a flexible and highly abstract advantages of the problem.
C + + supports two kinds of polymorphism: compile-time polymorphism and run-time polymorphism.
Compile-time polymorphism is obtained by overloading, while overloads have include function overloading and operator overloading.
Run-time polymorphism is obtained by using inheritance and virtual functions.

=================================== operator Overloading =============================================
1. What is the meaning of operator overloading?
When you define a new class, and you need to define what works for him, you need operator overloading to implement:
For example: Class a{...};
A B, c D;
d = b+c;//Compile times error because there is no + operation defined on type A.
A a::operator+ (a f) {};
D= b+c;//, that's right.
In fact, if you feel unaccustomed, you can write functions to implement related operations and invoke functions. For example, D=b.add (c);
Operator overloading is actually an overload of a special function, and essentially any one of the operators is a function at the bottom.
Operator overloading is to give multiple meanings to an existing operator so that different types of data from the same operator scope cause different behaviors to occur

2.c++ In addition to the following five operators cannot overload the remainder can be overloaded:
Member Accessors: "."
Scope Operator: "::"
Conditional operator: "?:"
Member pointer operator: "*"
Compile start of pre-processing command: "#"
3. Problems needing attention when operator overloading:
Operator Precedence and binding cannot be changed
The programmer does not create a new operator
The overloaded functionality is similar to the original function, and the number of operator operations data cannot be changed, and at least one operand is the operand of the custom type.
4. Operator overloading has two forms: in the form of a member function, in the form of a friend function

--------------The overloaded:< of the > member function-----------------------------
Note: If the overloaded operator is unary, then the argument list is empty, and if it is $ two, then there is an operand in the argument list. And so on The
operator is an essential keyword in operator overloading.
For example: String operator+ (const string &a), class declaration
String String::operator (const string &a) {...}; In-vitro definition
String Demo3 = demo1+demo2;<== equivalent to ==>string demo3=demo1.operator+ (DEMO2);
For detailed code, see: Book p125~129 "textbook".

Overloading <-----------------------------------for---------------> Friend functions
The most important difference between a friend function and a member function is that the number of argument lists is different
A friend function does not belong to any class, so it does not have the this pointer, so when it overloads the operator: the operator is unary so the argument list requires a
The operand, which is $ two, requires two parameters.
Overloads of the friend operator can return any type, but typically return the same type as its operation type.

Note: Some operators can either take the form of a member function overload, or it can be overloaded in the form of a friend function, but there is a situation where the UF meta function is only overloaded:
When the left operand of an operator is a constant, you cannot take advantage of the this pointer, you must use a friend overload.

5. Overloading of common operators:
Notice the difference between the pre-increment and post-increment expressions:
Price operator++ ()//This is the former self-increment
Price operator++ (int)//This is after self-increment

Simple summary of operator overloading for VC + +

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.