"operator"
at the time of the operation, if the closing parenthesis we know what to do first, then if there is no parentheses, arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, + + 、--operators, and so on, so many operators, we first, which is the precedence of these operators how to sort?
Priority Level |
operator |
Description |
Example |
Binding Nature |
1 |
() [] -> . :: + + -- |
adjust precedence bracket operator Array subscript access operator operator to access member by pointer to object operator to access member by object itself scope operator Post-increment operator Post-decrement operator |
(A + b)/4; Array[4] = 2; Ptr->age = 34; Obj.age = 34; Class::age = 2; for (i = 0; i <; i++) ... for (i = ten; i > 0; i--) ... |
left-to-right |
2 |
! ~ + + -- - + * & (type) sizeof |
logical take inverse operator Bitwise reverse (bitwise complement) Pre-increment operator Pre-decrement operator unary take negative operator unary take positive operator Dereference operator Fetch address operator Type conversion operator Returns the number of bytes occupied by an object |
if (!done) ... flags = ~flags; for (i = 0; i <; ++i) ... for (i = ten; i > 0; i.) ... int i =-1; int i = +1; data = *ptr; Address = &obj; int i = (int) floatnum; int size = sizeof (floatnum); |
Right-to-left |
3 |
->* . * |
operator that accesses a member on the pointer through a pointer to a member to access the member's operator on the object through a pointer to a member |
ptr->*var = 24; Obj.*var =; |
left-to-right |
4 |
* / % |
multiplication operator Division operator takes the remainder operator |
int i = 2 * 4; Float f = 10/3; int rem = 4 3; |
left-to-right |
5 |
+ - |
addition operator Subtraction operator |
int i = 2 + 3; int i = 5-1; |
left-to-right |
6 |
<< >> |
bitwise LEFT SHIFT operator Bitwise RIGHT SHIFT operator |
int flags = << 1; INT flags = >> 1; |
left-to-right |
7 |
< <= ; >= |
Less than the comparison operator less than or equal to the comparison operator is greater than the comparison operator greater than or equal to the comparison operator |
if (I < 42) ... if (i <= 42) ... if (i > 42) ... if (i >=) ... |
left-to-right |
8 |
== ! = |
equals the comparison operator does not equal the comparison operator |
if (i = = 42) ... if (i! =) ... |
left-to-right |
9 |
& |
bitwise AND operator |
flags = flags &; |
left-to-right |
10 |
^ |
bitwise XOR OR operator |
flags = flags ^; |
left-to-right |
11 |
| |
bitwise OR operator |
flags = Flags | |
left-to-right |
12 |
&& |
Logic and operator |
if (Conditiona && conditionb) ... |
left-to-right |
13 |
| | |
logic or operator |
if (Conditiona | | conditionb) ... |
left-to-right |
14 |
? : |
|
int i = (a > B)? A: b; |
Right-to-left |
15 |
= + = -= *= /= % = &= ^= |= <<= > >= |
assignment operator Compound Assignment Operator (addition) Compound assignment operator (subtraction) Compound assignment operator (multiplication) Compound assignment Operator (division) Compound assignment operator (take remainder) Compound assignment operator (bitwise AND) Compound assignment operator (bitwise XOR) Compound assignment operator (bitwise OR) Compound assignment operator (bitwise left) Compound assignment operator (bitwise right SHIFT) |
int a = B; A + = 3; B-= 4; a *= 5; a/= 2; a%= 3; Flags &= new_flags; Flags ^= new_flags; Flags |= new_flags; Flags <<= 2; Flags >>= 2; |
Right-to-left |
16 |
, |
Comma operator |
for (i = 0, j = 0; i <; i++, J + +) ... |
From left to right |
Well, there's so much on the table, and there's the same representation, for example,,& can be used both as the fetch address operator and as a bitwise AND operator. Instantly dizzy!
let's analyze and make this table simple.
First, look at the binding. There are two types: from left to right (most), right to left.
Then, look at the priority, a highest, a minimum, the lowest is a comma, this convenient memory, that the highest, what do you have in common? is to construct a type element or member and parentheses. ( There are some questions about this )
Second, the operator according to the operand, is not divided into monocular, binocular, trinocular operator. in the table, the Priority 2 is a single-mesh operator, the priority is 3~13 for the binocular operator, the priority is 14 for the three mesh operator, and the priority is 15 for the assignment operator.
But there are a lot of levels in the binocular operator, what about this? Then look! 3 counts as a pointer access member operator, with the arithmetic operator (first "multiplication" after "plus minus"), 6 as the shift operator, the 7~8 as the relational operator (first and after the size, then the equivalent), the 9~11 as a logical bitwise operator (first "and" after "or", "XOR"), 12~13 as the logical operator (first " With "after" or "). that is, arithmetic, relationship, logic. Shift, where the logical bits are inserted sequentially.
Finally, C + + operator precedence is: a highest (element or member of a constructed type, and parentheses); a minimum (comma); Pointer accesses a member row old three; one, two, three, assignment; binocular operator, arithmetic relation logic, shift, logical bit are inserted sequentially.
"operator overloading"
operator overloading is the representation of an object-oriented polymorphism, a mechanism that uses the same operator for different types of data to behave differently.
1, operator overloading condition: (except 5 kinds of operators, others can be overloaded)
. Member Selection Operators. * Member pointer operator:: Acting on the distinguished character?; Conditional Operatorssizeof calculate data size operator
These kinds of operators also belong to the more special category. (a
member selection condition is an operator that
acts on a
pointer to calculate the size of the
data )
2. Operator overloading rules (1 not, 3 unchanged)
(1) 1 cannot: cannot overload operators that are not in C + +
(2) 3 unchanged: original operation number, original semantics, original priority and binding.
3. Two forms of operator function overloading
(1) The friend function of overloading as class
friend function type operator operator (formal parameter list) {function body;}
(2) overloading a member function for a class
return type (class name ::) operator operator (formal parameter list) {function body;}
(3) comparison
member functions: monocular operator, type conversion function, operator action Modify object. friend function: binocular operator, operand implicit type conversion, operation has interactivity.
Note: binocular operator =,[], (),-> cannot be overloaded as a friend function for a class.
"Summary"
for C + + real problem on the operation, see more operators often do not know which side of the first, now summed up, is conducive to improve the efficiency of the problem. In the problem I have done, the operator overloading is usually on the selection question.
In fact, the memory of operator precedence is checked on the Internet, and then analyzed by itself. This makes me understand that no matter how much knowledge you see, you will find a connection if you find it, and you will be able to weave the knowledge network that belongs to you. So, encounter difficulties, do not be afraid, from a multi-angle analysis, everything can be solved.
"C + + Knowledge Point Summary" operator & operator overloading