C ++ primer study notes (c)

Nothing to say, is + - / & =, == and the like.

Left and right values

Briefly summarized: when a target value is used as the right, with the value (memory) of an object; when the object is left as the value with the identity (location in memory) of an object. P is assumed that a type of int *, if acting on the decltype when an expression such as decltype (* p) obtained is a reference type, the result decltype (& p) is an int **

Operator Precedence

Logical and relational operators

Wherein the logic AND operator (&&) and logical OR (||) operator is shorted evaluation operators:

  • For a logical AND operator, when and only true if left operand is evaluated only for the right operand.
  • For a logical OR operator, when and only if left operand is false when evaluated on the right operand.

Assignment Operators

  • Right assignment operator satisfies the associative law, i.e.,
1
2
int a,b,c;
a = b = c =3;//可以连续赋值
  • Calculating a lower priority assignment, it is in a conditional statement, the assignment portion is typically add brackets, such as: cpp int i; while((i=get_value())!=42){ //anything }

Increment decrement operators

Minute pre-release and post-release. Pre-release before adding 1, then the object after the change as a result of the evaluation, post version will also add 1 operands, but the result is a copy of the evaluation before the operand values ​​change.

If you want to test a true value arithmetic object or object pointer, the most direct approach is to condition it as if

1
2
3
4
if(val);//如果val是任意的非0值,条件为真
if(!val);//如果val是0,条件为真
有时我们试图将上面的真值测试写成如下形式:
ifval==true//只有当val等于1时条件才为真,这是因为,在进行比较之前会首先把true转换成val的类型,即ture换成1,false换成0

When comparing operation unless the object of comparison is a Boolean type, do not use Boolean literals true and false as operands.

Member access operator

1
ptr->mem等价于(*ptr).mem;

Conditional operator

? cond exprl: expr2
? example: String = finalgrade (Grade <60) "Fail": "Pass";
Grade <60 is true then take fail, then take false pass. The conditional operator precedence is too low, it may result in the following cases

1
2
3
cout << ((grade<60)? "fail : "pass");//输出pass或者fail
cout << (grade<60)? "fail : "pass";//输出1或者0!
cout << ((grade<60)? "fail : "pass");//错误:试图比较cout和60

Bitwise Operators

1 is the same or different exclusive 0

sizeof operator

sizeof operator returns the number of bytes occupied by one or expression of a type name, returns the value of a type size_t. Operator operand in two forms:
the sizeof (type)
the sizeof expr in the second form, sizeof returns the result of the expression of type sizes, the calculation is not calculated value of the object. sizeof operator results in part dependent on their type of action:

  • Sizeof operation is performed on the expression of char or char type, the result obtained 1
  • Sizeof operator performs reference to the type of the referenced object to obtain the size of the space occupied.
  • Sizeof pointer performs arithmetic to obtain the size of the space occupied by the pointer itself
  • Dereferencing the pointer to perform the sizeof object to obtain a pointer to the size of the space occupied by the pointer need not effective.
  • Sizeof operation performed to obtain an array size of space occupied by the entire array, the array is equivalent to all of the elements and the operation is executed once sizeof summing the results obtained. Note that, sizeof operation will not be converted into an array of pointers to process.
  • string and vector objects sizeof operation is performed on only the size of the return portion of fixed type, the object is calculated without the elements take up much space.

Comma operator

Comma operator contains two operands, left to right, are evaluated in order.
For comma operator, expression is evaluated first on the left, and then discard the evaluation result, the real result of the comma operator is the value of the expression on the right. If the right side is the left operand value, then the final result of evaluating the value is left.

Type Conversion

Implicit type conversion

Converted into an array of pointers: the most used expression array, the array automatically converted to point to the first element of the array pointer. When the array is used as a parameter decltype keyword, or taken as an address operator (&), sizeof operator and the like typeid operand when the conversion takes place. When initialized with a reference array to the conversion does not occur.

Pointer conversion: C ++ also provides several other pointer conversion mode, comprising a constant integer values ​​0 or literal nullptr can be converted into any pointer type; point to any great amount of a pointer can be converted to void *; point of any object pointers can converted into const void *.

Display conversion

Cast:

1
2
int i,j;
double slope = i/j;

Note : Although sometimes have to use a cast, but the essence of this approach is very dangerous.

Mandatory conversion named: cast-name <type> (expression);

Here, type is the target type conversion and expression is the value to be converted. If the type is a reference type, the result is an lvalue. cast-name is a static_cast, dynamic_cast, const_cast and in reinterpret_cast.

static_cast:

Any type converter having a well defined, they do not contain the underlying const, may be used static_cast. For example, by a double operand type cast to perform floating-point division can make the expression;

1
2
3
double slope = static_cast<double>(j)/i;
void *P=&d;//正确,任何非常量对象的地址都能存入void*
double *dp = static_cast<double*>(p);//正确,将void*转换回初始的指针类型,转换应确保转换后所得的类型就是指针所指的类型。否则将产生未定义的后果。

const_cast:

const_cast can change the operand underlying const

1
2
const char *pc;
char *p = const_cast<char*>(pc);//正确,但是通过p写值是未定义的行为

Note: For converting const object to const object's behavior, we called a "remove const nature." Once we removed the const properties of an object, the compiler will no longer prevent the object we write a. If the object itself is not a constant, use a cast to get written permission is unlawful. However, if the object is a constant, and then use const_cast write operations will have undefined consequences. Only constant change const_cast property expression, the use of other forms of naming cast constant change property expression will lead to a compiler error. Similarly, nor can const_cast change the type of expression:

1
2
3
4
5
string s="hello";
const char *cp=&s[0];
char *q = static_cast<char*>(cp);
static_cast<string>(cp);//正确:字符串字面值转换成string类型
const_cast<string>(cp);//错误:const_cast只改变常量属性

const_cast often used in the context of function overloading.

reinterpret_cast:

reinterpret_cast reinterpretation typically provided on the lower level bit pattern operands. For example, assume the following conversion

1
2
3
int *ip;
char *pc = reinterpret_cast<char*>(ip);/*我们必须牢记pc所指的真实对象是int而非字符,如果把pc当成普通的字符指针使用就可能在运行时发生错误。例如:*/
string str(pc);/*将会导致异常的运行时行为。使用reinterpret——cast是非常危险的。用pc初始化str的例子很好地证明了这一点。编译器无法知道pc实际指向的是int的指针。*/

Old cast

In earlier versions of c ++ language, an explicit manner as cast comprising two forms:

type (expr); // function form casts

(Type) expr; // c language-style cast

Depending on the type involved, the old and have cast const_cast, static_cast reinterpret_cast or similar function. If the replacement is not legal, the old cast reinterpret_cast performs a similar function.

Original: Big Box  C ++ primer study notes (c)


Guess you like

Origin www.cnblogs.com/chinatrump/p/11588810.html