C++ Primer 总结之Chap5 Expressions

本系列为本人温习C++基础时所记的tips,欢迎各位同学指正,共同进步TvT。

  1. An expression is composed of one or more operands combined by operators.

  2. Until we know the types of operands, it’s not possible to know what a particular expression means.

  3. 取余%时,现版标准是余数与被除数正负号相同,商向0取整

  4. When the precedent may be confusing, use parenthesis to assure correctness.

  5. We use ; here to separate operators with different precedents:

    • Relational and logical operators:!; <, <=, >, >=(left associative); ==, !=; &&; ||(from higher order to lower)
    • Bitwise operators:~; <<, >>; &; ^; |; How to deal with the sign bit of a signed integral value is machine-dependent, try to avoid it
  6. The shift operators(<<, >>) have lower precedence than the arithmetic operators but higher than the relational, assignment, or conditional operators… In a word, use () to get rid of confusion.

  7. The result and type of an assignment is the same as the left-hand operand. Assignment is right associative.int a; int* b; a=b=0; //errror, inconvertable

  8. -> is applied to pointers or iterators

    Myclass* mc = new Myclass();
    mc->mfun();
    (*mc).mfun();
    *mc.mfun();//error
  9. sizeof is an operator, NOT a function. Its value is decided at compile time and the operand expression will not be executed, only the type is concerned.

  10. Order of evaluation(求值顺序)is unspecified except &&, ||, ? :. The order of operand evaluation matters if one subexpression changes the value of an operand used in another subexpression.

    if (ia[ind++] < ia[ind]); // Undefined
    int *p1 = new int; // Uninitialized
    int *p2 = new int(); // 0, or say, null pointer
    mClass *p3 = new mClass; // Default Constr
    mClass *p4 = new mClass(); // Default Constr

    The () syntax for value initialization must follow a type name, not a variable name.

  11. new returns a pointer to the type specified.

    int i;
    int *p1 = &i;
    delete p1;// error, i is local
    int *p2 = 0;
    delete p2;// ok, have no effect
    int *p3 = new int;
    delete p3;// right

    Setting the pointer to 0 after delete to avoid the pointer being dangling, that is, refers to memory that once held an object but does so no longer.

  12. Three common program errors associated with dynamic memory allocation:

    • memory leak
    • dangling pointer, reading or writing to the object after it has been deleted
    • delete twice. This happens when two pointers point to the same dynamically allocated object, making the free store corrupted.
  13. When unsigned short meets int, if int is large enough to hold all the values of unsigned short, it’s converted to int; Otherwise, both of them are converted to unsigned int. This is machine dependent behavior. Other similar situations involving unsigned are like this.

  14. Conversions for expressions involving signed and unsigned int can be surprising: signed is converted to unsigned.e.g 0U > -1 is false because -1 is converted to a very large positive number in this case!!!

  15. A constant integral value of 0 can be converted to any pointer type; A pointer to any data type can be converted to a void*.

    char *p1 = false;// ok, false promoted to 0
    char *p2 = true;// error
  16. An explicit conversion: static_cast, dynamic_cast, const_cast, reinterpret_cast

    1. const_cast, remove constness.

    2. dynamic_cast, supports the tun-time identification of objects addressed either by a pointer or reference. Covered later.

    3. static_cast, any implicit conversion performed by compiler can be explicitly specified by it, to inform everyone that we are aware of this conversion.

    4. reinterpret_cast, rudely performs a low-level reinterpretation of the bit-pattern of its operands, strongly machine/compiler dependent.

Reference : C++ Primer 4th edition(评注版)

猜你喜欢

转载自blog.csdn.net/niyiweia/article/details/70674614
今日推荐