->
- it combine the dereference and member access.
- 引用成员 -
.
- 访问结构成员
- return 这个成员
[]
()
- call operator,用于启用函数
-
^4ded86- The minus sign is an operator that negates the value of its (literal) operand.
- 位运算
::
^a798ff- scope operator「resolver 域的解析符」, used to access names in a namespace
- when it has an empty left-hand side, it is a request to fetch the name on the right-hand side from the global scope.
Arithmetic
- left associative
- yield rvalue
- the operands to
%
must have integral type
Logical and Relational
- require and yield rvalue.
Assignment
- right associative and yield left-hand operand as lvalue. (可以使用连等号,但注意规矩,连起来的要么相同,要么可转化)
- if the right-hand operand is a braced initializer list (a curly brace) and it contain at most one value, that value doesn’t allowed a narrowing conversion.
Brevity Can Be a Virtue code-style
int i = get_value(); // get first value while(i != 42){ /* statements */ i = get_value(); // get next value }
int i; while((i = get_value()) != 42){ /* statements */ }
Increment and Decrement
- 符号在前,先加的值;符号在后,先值再加。
int i = 0, j;
j = ++i; // j=1, i=1
j = i++; // j=1, i=2
- prefix version yields lvalue, postfix version yields rvalue.
- Better to Use Prefix Version. code-style 因为后缀版会有一个保存原值的行为,而当使用复杂的iterator时,这个开销是很大的
Brevity Can Be a Virtue code-style
cout << *pbeg << endl; pbeg++;
cout << *pbeg++ << endl;
Conditional
cond ? expr1 : expr2
- guarantees only one of the expr is evaluated.
- result lvalue if the expr is lvalue.
- 优先级可低了,通常都是跟括号一起出现的
Bitwise
- take operands of integral type
- As usual, “small integer” will first promoted
- operands can be either signed or unsigned, but the way to handle “sign bit” is machine dependent. So, better use unsigned type
一些小技巧 (C++ Primer, p155)
获取某位on,不要算,而是
1UL << 27
on某一位,用or;off某一位,用and
quiz1 |= 1UL << 27; quiz1 &= ~(1UL << 27);
and和or都有保证别的不动,其中一个必动的方法,而且刚好这个必动是相反的
判断某一位是否on
bool status = quiz1 & (1UL << 27);
结合前面的判断,这里还可以有个or的反逻辑
sizeof
- result is a constant expression (编译时就决定了的值), type of
size_t
. (也因此可以用做定义数组的维数) - doesn’t evaluate its operand
- doesn’t convert the array to a pointer
以前的笔记
首先, 这不是函数, 是一个运算符
给出某个 类型 或 变量 在内存中所占的 字节数
int a=6; printf("sizeof(int)=%d\n",sizeof(int)); // sizeof(int)=4 printf("sizeof(a)=%d\n",sizeof(a)); // sizeof(a)=4 printf("sizeof(double)=%d\n",sizeof(double)); // sizeof(double)=8
sizeof是静态运算符, 它的结果在编译时刻就决定了 就是别在里头做运算, 不会算的
int a=6; printf("sizeof(a++)=%d\n",sizeof(a++)); // sizeof(a++)=4 printf("sizeof(a+1.0)=%d\n",sizeof(a+1.0)); // sizeof(a+1.0)=8
在编译的时候, 编译器读到sizeof就会看括号里的东西是多少字节, 再把这个数将整个sizeof替换掉, 所以在运行的时候, 括号里的表达式是没被运行的
Comma
- guarantee the Order of Evaluate: left to right
- 左侧表达式的结果被舍掉了,result是右侧表达式。whether lvalue depand on the r-h operand.