C _New_Basics - RicoJia/notes GitHub Wiki
-
Basics
- C++ 11 aka
c++0x
- 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
- 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
- Nice resources:
- Function prototype vs signature: signature = param types + return types; prototype = signature + function name (but no function body)
- C++ 11 aka
-
Compilation
- 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 egcmake . -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 thebuild_XXX
, script it will reset the build type back toRelWithDebInfo
. - 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:
- Unlike
- This drops the optimization to
- all codes are built with CMAKE_BUILD_TYPE=RelWithDebInfo optimization level.
- optimization
-
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 ========================================================================
- printing to stdout actually caches what to print, and it will flush when print cache is full
========================================================================
-
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.
-
scope resolution operator ::
- anonymous structure is NOT part of the standard
- :: here really is global variable
- Lexical Scoping {}, good for managing RAII resources (mutex locks), etc.
-
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 }
-
define
- by default, define gets a float
- BUT TO avoid problems, always a good idea to do const double instead of #define
- Example
```cpp
for (auto& i : ((std::vector<int>)*data)) printf("%d", i);
```