c ++ primer Fifth Edition Chapter IV Chapter V

The fourth chapter expression

About values

expression May be located '' = '' left result Objects do (left / right) Expressions
Lvalue expression v Objects, functions address
The right value of the expression x value

Case

&i;
i With the address of i, i is an lvalue
&i & I is a pointer, a pointer value, the value of & i is a right, such as int * p = & i
Lvalue Rvalue
decltype§ Reference types, eg.p is int *, int & return

Order of evaluation

f()+g()…

If several functions affect the same object, error

[!] So, if a f () function to change the value of an object, the expression g (), do not use this object. But not absolute. Exception: * ++ iterator, first ++ dereference again, there will be no exception.

Modulo

Operand m% n symbols
Integer And m are the same as

_ A reference to improve performance

Avoid copying of elements

Assignment Operators

Low priority

while((i=get_value())!=2){...}

= + _ Improve performance

Complex calculation only once

++ i_ improve performance

Compared i ++, i need to store

i ++ _ Applications

Convenient pointer traversal

while(){
    *p++;//先++,但是解引用的是p原值,不是p++后的值
    ...
}

Note - assignment expression

"=" Value on both sides of it can not be varied

while(){
*it=toupper(*it++);//error
}

"." And "->"

string s="dwa";
string *p=&S;
(*p).size();
p->size();

? :

string s=(..)?"dwa":"wda";

Bit computing

Operand
Integer, unsigned number recommended As bits
The standard library bitset
Operators
<< >> shift
~ Negated
And &, | or ^ XOR

sizeof

The number of bytes or expression of type

Return: size_t, constant expression

Two ways class members

sizeof(对象.成员);
sizeof(::成员);

Array size can be obtained

int a[]={1,2,3};
constexpr size_t size =sizeof(arr)/sizeof(*a);//计算数组大小
int arr2[size];//可以用来定义数组

"," Operator

for(;;,){}

Cast

const_cast functions: remove the underlying const

const char *p;
char *p2=const_cast<char*>(p);//p变成了char*,

The fifth chapter statement

throw expression

Function: anomaly detection

if(..){
	throw runtime_error("string");c
}

try block

Function: Exception Handling

try{

}catch(runtime_error err){
	cout<<err.what()<<endl;//
}catch(xxx){
}..
Published 19 original articles · won praise 0 · Views 1251

Guess you like

Origin blog.csdn.net/qq_35459198/article/details/105331435