C _New_Basics - RicoJia/notes GitHub Wiki

  1. Basics

    1. C++ 11 aka c++0x
    2. goals: fast, do things right; statically type safe; no resource leaks
      • So we have range checks, RAII, nullptr check, dangling pointer check
      • Hard real time programming can't use dynamic allocation freely
    3. How to compile: C++, you need to download IterativeMacros
      • g++ -std=c++14 -pthread A.cpp -o a.out
      • g++ -E A.cpp - see the preprocessed code
      • g++ -I /ABSOLUTE_PATH_TO LIB
    4. Nice resources:
    5. Function prototype vs signature: signature = param types + return types; prototype = signature + function name (but no function body)
  2. Compilation

    1. optimization
      • all codes are built with CMAKE_BUILD_TYPE=RelWithDebInfo optimization level.
        • moderatly aggressive optimizations (-O2) as well as keeping debug info (-g)
        • so you can get at least stack traces and be able to use a debugger at a basic level.
        • The -O2 can reorder operations/inline stuff/etc pretty drastically though so stepping through code may be difficult.
      • For better code stepping capabilities, then you can switch to CMAKE_BUILD_TYPE=Debug.
        • This drops the optimization to -O0. Alternatively, you can go into the build folder for the particular package and edit it with eg cmake . -DCMAKE_BUILD_TYPE=Debug
        • ? or via ccmake if you prefer a GUI. Note that if you do it this way, the next time you re-run the build_XXX, script it will reset the build type back to RelWithDebInfo.
        • GCC recently-ish added a -Og optimization level specifically for debbuggging.
          • Unlike -O0 which disables all optimizations,
          • -Og enables optimizations which don't interfere with stepping through code or analyzing the stack/memory. To take advantage of it, you can do something like the following:
  3. Printing

    • printing to stdout actually caches what to print, and it will flush when print cache is full
      • stderr does not cache
      • same thing with std::cout, but std::flush or std::endl will flush ========================================================================

Operators

========================================================================

  1. namespace: can be defined only in public scope see code

    • You might be wondering what's the difference from using. Using is for datatypes, not for namespaces.
  2. scope resolution operator ::

    1. anonymous structure is NOT part of the standard
    2. :: here really is global variable
    3. Lexical Scoping {}, good for managing RAII resources (mutex locks), etc.
  3. precedence

    • right associative and precedence: same precedence will go from right to left: *a++=*b++ 这个是 *(a++) = *(b++) 而 a++ 先return, 再evaluate。 所以: *a = *b; ++a;++b;
    • chart:
      • :: scope resolution
      • . -> [] member access a++ a--
      • ++a, --a; (so ++a++ will execute a++ first) & addr //so &ptr.another_ptr *ptr->another_ptr, are valid *a dereference
      • a + b // so *a + 1 is not *(a+1)
      • <<

      • |
      • = assignment, a?b:c
      • ,
    • Implementing a++:
      class Foo{
          public: 
          // Note that you're returning Foo here. 
          Foo operator ++(int);   // This is sucky but the only way to implement the postfix a++, by using a dummy
      }
  4. define

    • by default, define gets a float
    • BUT TO avoid problems, always a good idea to do const double instead of #define

TODO

- Example
    ```cpp
    for (auto& i : ((std::vector<int>)*data)) printf("%d", i);
    ```
⚠️ **GitHub.com Fallback** ⚠️