Skip to content

Instantly share code, notes, and snippets.

@wen-long
Last active August 29, 2015 14:03
Show Gist options
  • Save wen-long/f370cb8f7247e9716e59 to your computer and use it in GitHub Desktop.
Save wen-long/f370cb8f7247e9716e59 to your computer and use it in GitHub Desktop.

###C++ Primer第5版 学习笔记

####第四章

  1. 函数调用也是一种运算符,对运算对象的个数没有限制

  2. 重载运算符时,运算对象的个数,运算符的优先级和结合律都无法改变

  3. 左值表达式的求值结果是一个对象或者函数,但常量对象左值不能被赋值

  4. 赋值运算符需要左值作为左侧运算对象,结果也是左值

  5. 解引用,下标运算符求值结果为左值

  6. 如果表达式求值结果为左值,decltype 作用于该表达式得到引用类型 int *p;,decltype (*p)int &,decltype (&p)int **

  7. && || ?: ,这四种运算符规定了求值顺序,其他的都没规定,求值顺序与优先级,结合律无关

  8. 布尔值不应参与算数运算

    bool b = true;
    bool b2 = -b;//b2==true!!
    
  9. 商一律向 0 取整

  10. 推荐写法

    while ((i = get_value()) != 42) {
        /* balabala */
    }
    
  11. 箭头运算符返回左值还是右值取决于成员所属的对象是左值还是右值

  12. 不应将有符号数用于位运算,否则符号位的行为依赖于机器

  13. 移位运算的移动位数不能超过对象占用的位数

  14. sizeof 返回 size_t 类型,不会求值,无效指针也可

  15. 对数组 sizeof 返回数组占用空间大小,对 vector 则返回该类型固定部分的大小(比如我的编译器,vector 总是12)

  16. 强制类型转换

  • static_cast 可用于任何明确定义的类型转换,只要不包含底层 const 即可.
  • const_cast 只能改变底层 const,可用于重载函数
  • reinterpret_cast 用于位的低层次重新解释,本质上依赖于机器
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment