C++ Primer 总结之Chap5 Expressions

本文总结了C++ Primer中关于表达式的重点,包括运算符的优先级、取余规则、赋值操作、指针与迭代器的使用、类型转换以及内存管理中的常见错误。特别提醒注意求值顺序的不确定性以及混合使用signed和unsigned可能导致的意外行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本系列为本人温习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(评注版)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值