[C ++ knowledge point Summary] OPERATOR & amp; operator heavy load, knowledge point summary Operator

Source: Internet
Author: User
Tags bitwise operators

[C ++ knowledge point Summary] OPERATOR & operator heavy load, knowledge point summary Operator
[Operator]


When performing an operation, if we know which operation is performed first, if there are no parentheses, Arithmetic Operators, Relational operators, logical operators, bitwise operators, and value assignment operators, ++, -- operators, and so on. Which side do we calculate first, that is, how do we sort the priority of these operators?

Priority Operator Description Example Associativity
1 ()
[]
->
.
::
++
--
Parentheses operator for priority adjustment
Array subscript access operator
Access the member operator by pointing to the object pointer
Operator for accessing members through the object itself
Scope Operator
Post auto-incrementing Operator
Post-auto-subtraction Operator
(A + B)/4;
Array [4] = 2;
Ptr-> age = 34;
Obj. age = 34;
Class: age = 2;
For (I = 0; I <10; I ++ )...
For (I = 10; I> 0; I --)...
Left to right
2 !
~
++
--
-
+
*
&
(Type)
Sizeof
Logical anti-Operator
Bitwise inversion (bitwise population)
Pre-auto-increment operator
Front-End Auto-subtraction Operator
Negative operators for one element
Unary positive operators
Unreference Operator
Bitwise Operator
Type conversion Operator
Returns the number of bytes occupied by an object.
If (! Done )...
Flags = ~ Flags;
For (I = 0; I <10; ++ I )...
For (I = 10; I> 0; -- I )...
Int I =-1;
Int I = + 1;
Data = * ptr;
Address = & obj;
Int I = (int) floatNum;
Int size = sizeof (floatNum );
From right to left
3 -> *
.*
Access the operator of a member through a pointer to the member
Access member operators through pointers to members on objects
Ptr-> * var = 24;
Obj. * var = 24;
Left to right
4 *
/
%
Multiplication Operator
Division Operator
Returns 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 = 33 <1;
Int flags = 33> 1;
Left to right
7 <
<=
>
> =
Less than comparison operator
Less than or equal to the comparison operator
Greater than the comparison operator
Greater than or equal to the comparison operator
If (I <42 )...
If (I <= 42 )...
If (I> 42 )...
If (I> = 42 )...
Left to right
8 =
! =
Equal to the comparison operator
Not equal to the comparison operator
If (I = 42 )...
If (I! = 42 )...
Left to right
9 & Bitwise AND operator Flags = flags & 42; Left to right
10 ^ Bitwise OR operator Flags = flags ^ 42; Left to right
11 | Bitwise OR operator Flags = flags | 42; Left to right
12 && Logic and operators If (conditionA & conditionB )... Left to right
13 | Logic or operator If (conditionA | conditionB )... Left to right
14 ? : Ternary conditional Operators Int I = (a> B )? A: B; From right to left
15 =
+ =
-=
* =
/=
% =
& =
^ =
| =
<=
>>=
Value assignment operator
Compound assignment operator (addition)
Compound value assignment operator (subtraction)
Compound value assignment operator (multiplication)
Compound assignment operator (Division)
Compound value assignment operator (remainder)
Compound value assignment operator (bitwise AND)
Compound value assignment operator (bitwise XOR)
Compound value assignment operator (bitwise OR)
Compound value assignment operator (shifts left by bit)
Compound value assignment operator (shifts right by bit)
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;
From right to left
16 , Comma Operator For (I = 0, j = 0; I <10; I ++, j ++ )... Left to right


There are so many identical representations in the table. For example, & can be used as both an address operator and a bit and an operator. Dizzy in an instant!
Let's analyze and make the table simple.
First, look at the combination. There are two types: from left to right (mostly), from right to left.
Then, let's look at the priority. The highest, the lowest, and the lowest is the comma. This is easy to remember. What do we have in common? It is the element or member of the construction type and the brackets. (There are still some questions)
Secondly, operators are classified into single object, binary object, and three object operators based on the operands. In the table, if the priority is 2, it is a single object operator, and the priority is 3 ~ 13 is a binary operator, 14 is a three-object operator, and 15 is a value assignment operator.
But there are still many levels of binary operators. What should I do? Let's take a look! 3 is counted as a pointer access member operator, 4 ~ 5 is an arithmetic operator (first "multiplication, division, and remainder" and then "addition and subtraction"), 6 is a shift operator, 7 ~ 8 is a relational operator (equal to or equal to the ratio), 9 ~ 11 is a logical bitwise operator (first "and" then "or", and then "exclusive or" in the middle), 12 ~ 13 is the logical operator (first "and" then "or "). That is, arithmetic, relational, and logic. Shift, where the logic bit is inserted in order.
Finally, the priority of the C ++ operator is: the highest (element or member of the constructor type and parentheses), the lowest (comma), and the third (One, Two, Three, value assignment; binary operator, arithmetic Relation Logic, shift, logical Bit Insert in order.

[Operator Overloading]
Operator Overloading is a manifestation of polymorphism in objects. It is a mechanism that uses the same operator to act on different types of data with different behaviors.
1. Operator overload conditions: (except for the five operators, other operators can be overloaded)
. Member selection operator. * member pointer OPERATOR: does it apply to delimiters ?; Conditional operator sizeof calculate data size Operator
These operators are also a special type. ( Member Selection ConditionsYes FunctionYu PointerTo Calculate data size)
2. Operator overload rules (1 is not allowed, 3 is not changed)
(1) 1 NO: operators not available in C ++ cannot be overloaded.
(2) 3 unchanged: Number of original operations, original semantics, original priority, and combination.
3. Operator function Overloading
(1) overload as a friend function of the class
Operator (parameter table) {function body ;}
(2) Reload as a member function of the class
Return type (Class Name:) operator (parameter table) {function body ;}
(3) Comparison
Member functions: single-object operators, type conversion functions, and operator operations to modify objects. Youyuan functions: Binary operators, which are implicitly converted to the type of operands, provide interactive operations. Note: The binary operator =, [], (),-> cannot be a friend function of a class.
[Summary]
I often don't know which side to calculate if there are many operators for the Operation questions on the C ++ real question. Now I will summarize the operations, which will help improve the efficiency of the question. Among the questions I have done, the operator overload usually lies in the choice question.
In fact, the memory of operator priority is checked online and analyzed by yourself. This makes me understand that no matter how much knowledge you see, as long as you find it, you will be able to find a connection, and can weave your knowledge network. Therefore, if you encounter difficulties, do not be afraid to analyze them from multiple perspectives. Everything can be solved.

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.