Because I encountered a problem when I wrote a small program myself, it is not acceptable for some operator to be defined as a member function, but it is OK to define it as a friend, and some do not understand it, it is
Operator priority table of C ++
Precedence
Operator
Description
Example
Associativity
1
()[]->.::++--
Grouping OperatorArray accessMember access from a pointerMember access from an objectScoping
The following is an introduction to the C ++ operator's heavy-duty member functions and youyuan functions. If you need them, please refer to them.Copy codeThe Code is as follows:# Include Using namespace std;Class{Int x, y;Public:A (int xx, int yy):
View code
/*Operator overload, type conversion function, conversion ConstructorNo parameter default constructor, with parameter initialization constructor,*/# Include // # Include # Include // Using namespace STD;Class Complex{Public:Complex ()
The class declaration of the overload OPERATOR:
Struct vector {public Double X, Y, Z; Public vector (Double X, Double Y, Double Z) {This. X = x; this. y = y; this. z = z;} public vector (vector RHs) {x = This. x; y = This. y; Z = This. z;} public
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]
The assignment operator is also used together with the arithmetic operator. It is mentioned in the appendix earlier. For example, to add x to 4 and then assign the value to x, you can write it as x + = 4.
Copy codeThe Code is as follows: public
C # can also be used to perform vector operations, which solves a long-lasting doubt.operator structVector { Public Doublex, y, Z; PublicVector (DoubleXDoubleYDoublez) { This. x =x; This. y =y; This. z =Z; } Publicvector (vector rhs) {
Arithmetic operatorsPlus minus +- from left to right +- siblingMultiplication modulus *%/ from left to right above +-Self-added + + +-- right binding higher than basic arithmetic operatorPositive and negative -+ from right to left with + +--
Priority level1,[], (),.,--from left to right2,-,~,++,--,*,&,! , (type), sizeof () from right to left. The monocular operator.3,/, *,% binocular operator4, +,-5, > shift6, 7, = =,! =8-12, &,^, |, &&, | | logical operators13,?: Right to Left14,=, *=,%
TheValue assignment operator overload FunctionThe function is similar to that of the built-in value assignment operator. However, you must note that it is the same as the copy constructor and the destructor. Pay attention to the issue of deep copy
Sometimes you need to reload the operator to write a class, but some operators (such as =, ) can be written in the class or out-of-class. So how will the C ++ compiler call it?
First, we know that the "overload" mechanism allows multiple functions
[Project 2 Expansion 2 (thinking)] This question hangs your appetite: sets two score class objects, such as CFraction c1 and c2. If int I is defined, we can use cin> I> j; enter the values of I and j on the keyboard, whether to use cin> c1> c2; what
A single operator is used to return a reference. It cannot be a friend function. Otherwise, the compilation fails.
Double operator, which must be defined as youyuan; otherwise, compilation fails.
Test Compiler: g ++ 4.6.3
[Cpp]# Include #
Operator is a C + + keyword that is used in conjunction with an operator to represent an operator function that should be understood as a function name as a whole operator=.This is a method of the C + + extension operator function, although it looks
Priority level
Operator
Name or meaning
Use form
Combination direction
Description
1
[]
Array subscript
array name [constant expression]
Left to right
()
Operator overloading is essentially an overload of a function
The functions of overloaded operators are generally formatted as follows:
function type operator operator name (parameter list column)
{Overloaded handling of operators}
For example,
1, nullable type modifier (?) : A reference type can use a null reference to represent a nonexistent value, and a value type is usually not represented as empty, for example: string str=null; is correct. int i=null; The compiler will make an error.
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.