跳至主要内容

Ch1

Item 1: View C++ as a federation of languages

  • In the beginning, C++ was just C with some oo features tacked on.
  • Become modern
    • Exceptions required different approaches to structuring function. [#Item 29]
    • Templates[#Item 41]
  • Now C++ is a multiparadigm programming language
    • Combination of procedural, oo, functional, generic, and metapgrogramming features.
  • View c++ as a federation of related languages.
  • Four subranges
    • C
      • Builtin data types, pointer, arrays.
    • Object-oriented C++
      • C tiwh classes.
      • Encapsulation, inheritence, polymorphism, v-function.
    • Template C++
      • common
      • TMP(Template metaprogramming)
    • STL
      • A template library.

Item 2: Prefer consts, enums, inline to #defines

  • Prefer compiler to preprocesser

    • #defines is handled by preprocessor, not compiler.
  • #define can cause problem because it just doing replacement.

    • Use const instead
  • 2 special cases when using #define

    1. const pointer
    2. const class member
  • use inline instead of #define func

Item3: Use const whenever possible

  • mutable variable: use when a const member function need to modify a data member.

Item4: Make sure that objects are initialized before they're used.