当前位置:网站首页>Chapter IV expression

Chapter IV expression

2022-06-12 13:52:00 Doll noodles I

1. Left and right

To sum up briefly : When an object is used as an R-value , Using the value of the object ( Content ). When an object is used as an lvalue , It's the identity of the object ( Location in memory . Address )

Where an R-value is required, an l-value can be used instead , But the right value cannot be used as the left value

2. Order of evaluation

int i = 0;
cout << i++ << " " << i << endl;	//0 1
cout << i << " " << i++ << endl;	//1 0
cout << i << " " << ++i << endl;	//1 1
cout << ++i << " " << i << endl;	//1 1

3. remainder

// Symbols only look at the remainder 
-3 % 2 = -1;
3 % -2 = 1;
// Floating point number can't take remainder , And the remainder cannot be 0

4. Operator priority

 Logic is not ! >  The arithmetic ( Add, subtract, multiply and divide ) >  Relationship ( Greater than or equal to but not equal to ) >  Logic and && >  Logic or || >  assignment =;
* Priority of dereference  <  Front and back ++;
* Priority of dereference  < .;

cout << *it++;
 First ++, After ++ Will return to the original it;
 Dereference is to dereference the original it;

5. Right association law , Multiple variables are assigned at the same time

int a, b, c;
a = b = c = 0;

int i;double d;
i = d = 3.5; //3 3.5
d = i = 3.5; //3 3

6. Assignment loop optimization writing

// before 
int i = get_value();
while(1 != 42)
{
    
    ...
    i = get_value();
}
// Now? 
int i;
while((i = get_value) != 42)
{
    
    ...
}

7. sizeof

Satisfy the right union law . The resulting value is a size_t type

sizeof *p;  // Right association law , Operation is equivalent to sizeof(*p). And don't care p Value , So whatever you return is 4 

Be careful :

  1. When a character array represents a string , Its sizeof Value will ’/0’ Count it in .(strlen Can't calculate ’\0’, And the terminator is encountered ’\0’ stop it )
  2. When the array is a formal parameter , Its sizeof The value is equivalent to the... Of the pointer sizeof value .

8. Transformation in type

bool -> char -> unsigned char -> short -> unsigned short -> int -> long	 -> float -> double

9. Cast

//type For the target data type ,expression A variable or expression of the original data type 
cast-name<type>(expression);
cast-name = [static_cast | dynamic_cast | const_cast | reinterpret_cast];

1.static_cast

It's equivalent to the traditional C Coercion in language , Can be cast to any type except the following ,

Can't switch off expression Of const、volatile、 perhaps __unaligned attribute .

2.const_cast

const_cast, Used to modify the type of const or volatile attribute .

This operator is used to modify the const( The only one with this ability C+±style Transformation operators ) or volatile attribute . except const or volatile Beyond modification , new_type and expression Of the same type .

  1. Constant pointers are converted into non constant pointers , And still point to the original object ;
  2. Constant references are converted to non constant references , And still point to the original object ;
  3. const_cast It is generally used to modify the bottom pointer . Such as const char *p form .
const int g = 20;
//int *h = &g;
int *h = const_cast<int *>(&g);
*h = 52;
cout << g + 10;  //30
cout << *h + 10; //62

3.reinterpret_cast( It is not recommended to use )

type It must be a pointer 、 quote 、 Arithmetic Type 、 Function pointer or member pointer .

It can convert a pointer to an integer , You can also convert an integer to a pointer ( First convert a pointer to an integer , Then convert the integer to the pointer of the original type , You can also get the original pointer value ).

原网站

版权声明
本文为[Doll noodles I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010514468928.html