2017年7月1日 星期六

ch 5 Expression & conversion

5.3 bitwise 為bit操作運算值; 

such as: << , & , |

5.5 Increment and Decrement


有分為prefix and postfix , such as ++i  or i++
注意postfix 同一行中,會用先前值來處理
例如 :

int count=0;
vector.push(count ++) //存值為 0
int count=0;
vector.push(++count ) //存值為 1


5.10 compound Expressions 複合式


主要有兩點考量1.優先值 precedence 2.結合順序 associativity

http://en.cppreference.com/w/cpp/language/operator_precedence
PrecedenceOperatorDescriptionAssociativity
1::Scope resolutionLeft-to-right
2a++   a--Suffix/postfix increment and decrement
type()   type{}Functional cast
a()Function call
a[]Subscript
.   ->Member access
3++a   --aPrefix increment and decrementRight-to-left
+a   -aUnary plus and minus
!   ~Logical NOT and bitwise NOT
(type)C-style cast
*aIndirection (dereference)
&aAddress-of
sizeofSize-of[note 1]
new   new[]Dynamic memory allocation
delete   delete[]Dynamic memory deallocation
4.*   ->*Pointer-to-memberLeft-to-right
5a*b   a/b   a%bMultiplication, division, and remainder
6a+b   a-bAddition and subtraction
7<<   >>Bitwise left shift and right shift
8<   <=For relational operators < and ≤ respectively
>   >=For relational operators > and ≥ respectively
9==   !=For relational operators = and ≠ respectively
10a&bBitwise AND
11^Bitwise XOR (exclusive or)
12|Bitwise OR (inclusive or)
13&&Logical AND
14||Logical OR
15a?b:cTernary conditional[note 2]Right-to-left
throwthrow operator
=Direct assignment (provided by default for C++ classes)
+=   -=Compound assignment by sum and difference
*=   /=   %=Compound assignment by product, quotient, and remainder
<<=   >>=Compound assignment by bitwise left shift and right shift
&=   ^=   |=Compound assignment by bitwise AND, XOR, and OR
16,CommaLeft-to-right

與優先順序有關的例子:
ex1:
vector<int> testVector;
testVector.push_back(1);

vector<int> *pVector=&testVector;
(*pVector)[0] // correct
*vector[0] //error


5.12 Type Conversion


1. Implicit 隱式 2.Explicit 顯式 3.Arithmetic 運算

1.Implicit conversion

int ivalue=3.14; // double -> int


2.Explicit  conversion

static_cast, dynamic_cast, const_cast, reinterpret_cast

3.Arithmetic  conversion
比int 小的 such as : char ,short, will promte to int;(integral promotions)
例如:
int ivalue=1; double dval=3.111;

ivalue+dval 則ivalue 變為double

沒有留言:

張貼留言