Today, I carefully read the previous examples of Objective C ++ and more objective C ++ in the reference books, focusing on the practical methods of operator overloading, it looked great, and I did it myself. The compiler uses VC ++. Net 7.1 because
Why overloading an operator:The operands of operators in C + + predefined are limited to the basic built-in data types, but there is no way to manipulate our custom types (classes). But most of the time we need to perform similar operations on the
Why do I need a conversion operator?
We know that for built-in types of data we can convert data by forcing the use of a translator, such as (int) 2.1f; a custom class is also a type, so objects in a custom class also need to support this operation
Today I saw operator overloading in C + + and logged it for later review:#include using namespace Std;class f{int n;int d;void reduce () {int mcd = MAXCD (n d =-this->d;this->n =-this- >n;} Reduce (); cout Note the point:1. Anonymous objects2.
The assignment operator also has the same usage as the arithmetic operator, as mentioned in the Appendix: for example, to add 4 to X and then assign it to x, you can write x+=4.
Copy Code code as follows:
public class mathops{
public
Operator overloading is an important part of C + +, which makes programs easier to understand and simple operators use to make complex functions more intuitive.
It is natural for ordinary objects to use the arithmetic operators frequently to get
In fact, many C + + operators have been overloaded. Eg: using the * operator for an address will get the value stored in this address, and when used for 2 digits, it will be their product. C + + determines which action to take based on the number
Copy Code code as follows:
#include
using namespace Std;
Class A
{
int x,y;
Public
A (int xx,int yy): X (xx), Y (yy) {}
A () {x=0;y=0;}
A operator+ (const AB)//No const limit, you can also
{return A (X+B.X,Y+B.Y);}
A operator-()
The type converter takes a common form:
Operator/* Overload type */() {/
* content */
}
Any return type that can be a function (except void) can be overloaded
converting to an array or function type is not allowed, and it is possible to
A type conversion operator is a special class member function that defines a transformation that converts a class type value to another type value. The conversion operator is declared in the class definition body, followed by the target type of the
Arithmetic operators+: Two operands added, example: 2+3 of 5-: The first operand minus the second operand example: 5-3 of 2*: Two operands multiplied, example: 2*3 of 6/: Numerator divided by denominator, example: 5/2 to 2%: modulo operator,
Precedence
Operator
Description
Example
Associativity
1
()[]->.::++--
Grouping OperatorArray accessMember access from a pointerMember access from an objectScoping OperatorPost-IncrementPost-Decrement
(A + B)/4;Array [4]
Precedence
Operator
Description
Example
Associativity
1
()[]->.::++--
Grouping OperatorArray accessMember access from a pointerMember access from an objectScoping OperatorPost-IncrementPost-Decrement
(A + B)/4
We often perform arithmetic operations. Comparison operations are actually an overloaded operator, but they only correspond to common data types. If we want to perform operations on the class and struct, what will happen? The compiler does not know.
C # mainly supports the following operators:
Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Arithmetic Operator +-*/%Logical operators ~ & |!String concatenation operator +Increment and
I. Why use Operator Overloading?For all operators of the system, generally, only the basic data types and classes provided in the standard library are supported. for user-defined classes, if you want to support basic operations, such as comparing
1. Use of priority OperatorsI believe that all people who have played C have had such or such incredible experiences, and they will feel very deeply profound and profound about C, most of the time, you do not know the actual execution of a statement
This article mainly discusses Operator overloading, function objects, and class type conversion in C ++.
If there are any errors or omissions in this article, please point out. Thank you! The overload operator is a function with a special name: the
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.