Pointers in modern c++

  • What do they do?

    • Varible that store addresses of other varibles (their location in ram)
    • Pointer declaration/initialization
    • Adress (&) and indirection (*) pointer opnerators
    • Pointers vs. refrences
    • Pass by refrence with pointers
    • Built-in pointer-based arrays
    • Pointer-based strings
    • Use const with pointers and the data they point to
  • Pointer problems

    • Powerful but challenging to work with
    • Modern C++ features make most use of pointers obsolete
    • new software development projects
      • use references rather than pointers
      • use std::array and std::vector rather than built in pointer-based arrays
      • used std::string objects to pointer-based c-strings
    • C++ 20 features to avoid pointers

      • to_array converts pointer based array to a more robust std::array
      • spans safer way to pass built in arrays to functions
        • iterable, (so you can use them with range based for)
        • also can use them with standard library container-processing algorithms
      • key takeaway
        • avoid using pointers, pointer-based arrays and pointer based strings when ever possible
          • if you have to use them take advantage of to_array and spans
  • When pointers are required

    • Lagacy code
    • dynamic data structures
      • though most programmers will use the c++ standard library’s existing dynamic containers
    • command-line arguments
    • pass arguments by refrence if there’s a possiblilty of a nullptr

Pointer Declarations and initialization

xo.