Zerglurker C-Language tutorial 009--operator details (i)

Source: Internet
Author: User



In the previous sections we talked about data types, functions, code Execution order, and some ways to add simple functions.



In this section we will focus on operators. Includes the meaning of operators and the concept of precedence



In the C language, the following operators are recognized:





A detailed explanation of C + + language operators
Priority level Operator Name and Meaning Operational accounts Using the example Combination direction Can overload Additional Instructions
No () Round Brackets Monocular (expression) No Whether Expression in parentheses is always calculated first
No dynamic_cast<> () Type dynamic Conversions Monocular dynamic_cast< target type > (source) No Whether C + + proprietary, cannot convert return null
No static_cast<> () Type static conversions Monocular static_cast< target type > (source) No Whether C + + proprietary, no detection conversion "NOTE 1"
No reinterpret_cast<> () Type Interpretation transformation Monocular reinterpret_cast< target type > (source) No Whether C + + proprietary, cast "Note 1"
No const_cast<> () Go to constant conversion Monocular const_cast< target type > (source) No Whether C + + proprietary, you can turn constants to very much
0 :: Scope resolution Binocular Domain Name:: Target Name From left to right Whether C + + Proprietary
1 [] Subscript operator Binocular Array name [expression] From left to right Can Both languages have
1 () Function call Binocular Function name (parameter) From left to right Can Both languages have
1 . Object member Selection Binocular Object. Member From left to right Whether Both languages have
1 - Pointer member selection Binocular Member, object pointer From left to right Whether Both languages have
1 {} Combinatorial operations Unknown {statement 1; statement 2; .... statement N;} From left to right Whether Both languages have
2 ++ Self-increment operation Monocular Variable + +
+ + variable "NOTE 2"
From left to right
From right to left
Can Both languages have
2 -- Self-subtraction operations Monocular Variable--
--Variable "NOTE 2"
From left to right
From right to left
Can Both languages have
2 + Positive sign Monocular + Variable From right to left Can Both languages have
2 - Minus sign Monocular -Variable From right to left Can Both languages have
2 ! Logical Non- Monocular ! An expression From right to left Can Both languages have
2 ~ Bitwise REVERSE Monocular ~ Variable From right to left Can Both languages have
2 * Pointer dereference Monocular * pointer From right to left Can Both languages have
2 & Fetch Address Monocular & Variables From right to left Can Both languages have
2 Type Forcing type conversions Monocular (type) source From right to left Can Both languages have
2 sizeof Get variable occupancy
Space size Memory
Monocular sizeof (source) From right to left Whether Both languages have
2 New/new[] Allocating memory Monocular New type name or new[] type name From right to left Can C + + Proprietary
2 Delete/delete[] Freeing memory Monocular Delete pointer or delete[] pointer From right to left Can C + + Proprietary
3 .* Member Object Selection Binocular Object. * Member name or
Object. * Member Pointers
From left to right Can C + + Proprietary
3 ->* Member Pointer selection Binocular The object pointer->* the member name or
Object pointer->.* member pointer
From left to right Can C + + Proprietary
4 * Multiplication Binocular Variable 1* variable 2 From left to right Can Both languages have
4 / Division Binocular Variable 1/Variable 2 From left to right Can Both languages have
4 % Take surplus Binocular Variable 1% variable 2 From left to right Can Both languages have
5 + Addition Binocular Variable 1+ variable 2 From left to right Can Both languages have
5 - Subtraction Binocular Variable 1-Variable 2 From left to right Can Both languages have
6 << Bitwise LEFT Shift Binocular Variable 1<< variable 2 From left to right Can Both languages have
6 >> Bitwise RIGHT SHIFT Binocular Variable 1>> variable 2 From left to right Can Both languages have
7 < Less than Binocular Variable 1< variable 2 From left to right Can Both languages have
7 <= No greater than Binocular Variable 1<= variable 2 From left to right Can Both languages have
7 > Greater than Binocular Variable 1> variable 2 From left to right Can Both languages have
7 >= Not less than Binocular Variable 1>= variable 2 From left to right Can Both languages have
8 == Logic, etc. Binocular Variable 1== variable 2 From left to right Can Both languages have
8 != Not equal to Binocular Variable 1! = Variable 2 From left to right Can Both languages have
9 & and arithmetic Binocular Variable 1& variable 2 From left to right Can Both languages have
10 ^ XOR or operation Binocular Variable 1^ variable 2 From left to right Can Both languages have
11 | or arithmetic Binocular Variable 1| variable 2 From left to right Can Both languages have
12 && Logic and Binocular Variable 1&& variable 2 From left to right Can Both languages have
13 || Logical OR Binocular Variable 1| | Variable 2 From left to right Can Both languages have
14 ?: Conditional operations Tri-Mesh Variable 1? Variable 2: Variable 3 From right to left Whether Both languages have
15 = Assign value Binocular Variable 1 = variable 2 From right to left Can Both languages have
15 += Add after Assignment Binocular Variable 1+= variable 2 From right to left Can Both languages have
15 -= Reduced-value Assignment Binocular Variable 1-= variable 2 From right to left Can Both languages have
15 *= Multiply post-Assign value Binocular Variable 1*= variable 2 From right to left Can Both languages have
15 /= Assign value after addition Binocular Variable 1/= variable 2 From right to left Can Both languages have
15 %= Post-remainder assignment value Binocular Variable 1%= variable 2 From right to left Can Both languages have
15 <<= Left Shift Assignment Binocular Variable 1 <<= variable 2 From right to left Can Both languages have
15 >>= Shift Right Assignment Binocular Variable 1 >>= variable 2 From right to left Can Both languages have
15 &= and post-assignment values Binocular Variable 1 &= variable 2 From right to left Can Both languages have
15 ^= XOR/Assign Value Binocular Variable 1 ^= variable 2 From right to left Can Both languages have
15 |= or post-assignment value Binocular Variable 1 |= variable 2 From right to left Can Both languages have
16 Throw Throw exception Monocular Throw Variable 1 From right to left Whether C + + Proprietary
17 , Comma operation Binocular Variable 1, variable 2 From left to right Can Both languages have

























































































































































































Note 1:reinterpret_cast<> () andstatic_cast<> () all seem to be casts, but the two are different



Observe the following code:





class A {
public:
	A(){ m_a = 1; }
	int m_a;
};

class B {
public:
	B(){ m_b = 2; }
	int m_b;
};

class C : public A, public B 
{
};

void OperatorPriority()
{
	
	C c;
	printf("%p, %p, %p\n", &c, reinterpret_cast<B*>(&c), static_cast <B*>(&c));
	B* b1 = &c, *b2 = reinterpret_cast<B*>(&c), *b3 = static_cast <B*>(&c);
	printf("%d, %d, %d\n", b1->m_b, b2->m_b, b3->m_b);
}

After executing the operatorpriority () function, the output is as follows:








What does that mean?



This means that at the time of the conversionreinterpret_cast<> () is a no-brain conversion, completely regardless of the cause and the consequences, to cast



andstatic_cast<> () will try to match first, can match will make processing, otherwise cast



So M_bstatic_cast<> () will output the correct value, andreinterpret_cast<> () will give an error value



But why is forcing type conversion to output M_b correctly? This involves the polymorphic nature of C + +, which I'll talk about later. Only operators are discussed here, not in-depth explanations.



Note 2: Prefix increment/decrement operation and suffix increment/decrement operation



The prefix increment/decrement is such that the + + variable--variable



The suffix increment/decrement is such a variable + + variable--



There is a lot of information that suffix increment/decrement operations have precedence over prefixes



This is too simple to say, the actual situation is very complex (that is, this statement is completely a head, no practice of the nonsense!) )



First, look at the code:





From the above code can be clearly seen, if you think the suffix of the increment operation has so-called high priority, then completely does not make sense.





First of all, the statement is a grammatical error ++a--! The prefix and postfix operators cannot be performed at the same time, so they cannot be compared to their precedence.



However, this does not mean that the priority descriptions on the Web are wrong, because we can compare them with other operators of their peers.



Note the TEST002 Function! The operation is an operator of the same class as the prefix increment operation, which is supposed to be calculated by first calculating the suffix increment and then calculating the non,



This should be equivalent to a=! (a++) that the result should be 0. But why is the output 1?



The answer is complicated. The actual operation above is done as follows:



Calculate the calculation first a++ namely 0++=1 Save the result to a and then follow a=0 calculate!a namely!0=1, save result again to a result is a finally become 1



Here is the disassembly of the Code





	a = !a++;
00113EBD 8B 45 F8             mov         eax,dword ptr [a]  
00113EC0 89 85 30 FF FF FF    mov         dword ptr [ebp-0D0h],eax  
00113EC6 8B 4D F8             mov         ecx,dword ptr [a]  
00113EC9 83 C1 01             add         ecx,1  
00113ECC 89 4D F8             mov         dword ptr [a],ecx  
00113ECF 83 BD 30 FF FF FF 00 cmp         dword ptr [ebp-0D0h],0  
00113ED6 75 0C                jne         main+0B4h (0113EE4h)  
00113ED8 C7 85 2C FF FF FF 01 00 00 00 mov         dword ptr [ebp-0D4h],1  
00113EE2 EB 0A                jmp         main+0BEh (0113EEEh)  
00113EE4 C7 85 2C FF FF FF 00 00 00 00 mov         dword ptr [ebp-0D4h],0  
00113EEE 8B 95 2C FF FF FF    mov         edx,dword ptr [ebp-0D4h]  
00113EF4 89 55 F8             mov         dword ptr [a],edx
On the surface, suffix + + appears to be the preferred operation. But we can't just focus on this, but focus on its impact on the results.
The operation is not calculated first, it is high priority--if the operation is not able to affect the result
The result of the operation of this expression a++ is discarded, that is! The operator masks the suffix ++!!!




Does it feel a little messy? No matter, we look at other functions, so you will be more messy!


See function test003 test004



The calculation of 003 is carried out as follows: The first calculation A+a namely 0+0 obtains 0, then calculates the increment namely 0++ obtains 1. Last assignment, take the value of a++ 1



The following is the actual calculation process





00DD3E55 8B 45 F8             mov         eax,dword ptr [a]  
00DD3E58 03 45 F8             add         eax,dword ptr [a]  
00DD3E5B 89 45 F8             mov         dword ptr [a],eax  
00DD3E5E 8B 4D F8             mov         ecx,dword ptr [a]  
00DD3E61 83 C1 01             add         ecx,1  
00DD3E64 89 4D F8             mov         dword ptr [a],ecx 
In other words, the a+a is calculated first, and then the a++





This is not a description of the + operator priority higher than the suffix + +!!



The calculation of 004 is carried out as follows: First calculate 2+a namely 2+0 get 2, then calculate self-increment, namely 2++, get 3





01383E89 8B 45 F8             mov         eax,dword ptr [a]  
01383E8C 83 C0 02             add         eax,2  
01383E8F 89 45 F8             mov         dword ptr [a],eax  
01383E92 8B 4D F8             mov         ecx,dword ptr [a]  
01383E95 83 C1 01             add         ecx,1  
01383E98 89 4D F8             mov         dword ptr [a],ecx 
Get the same logical result + operator precedence higher than suffix + +





Now let's sum it up:



Postfix operator and! operator together, the suffix operator is masked



When the postfix operator and the +-*/operator are together, the other is computed, and the self-increment



Now, are you completely confused?



Good, that's right.



Because we now have a conclusion:



1 those so-called priorities are all nonsense, and don't expect priorities to be executed exactly as you expect them to.



2 Please arrange the priority with parentheses-only This is the only reliable partner!



At the beginning of the next section I will analyze and explain the operators one by one, so please look forward to ...
















































Zerglurker C-Language tutorial 009--operator details (i)


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.