This is reading notes on Professional C++ by Marc Gregoire, but also includes other notes.
- In switch statement, expression must be integer type.
- Why can’t be string? Because C++ does not recognize string as a type. Ref:
- Array initialization
- in C style:
- int myArray[3] = {0}; // all 0
- int myArray[3] = {1}; // first 1, other two 0
- int myArray[] = {0,1,2}; // an array of 3 elements, [0;1;2]
- in C++ style:
- array<int, 3> myArray = {0, 1, 2}
- For array of variable length, use vector
- in C style:
- Range-based for loop
- for (int i : myArray)
- __func__ is implicitly declared (i.e. can be used as function name directly inside of a function) as:
- static const char __func__[] = “func-name”;
- Alternative function syntax (with trailing return type):
- auto func(int i) -> int // {…}
- Function return type deduction (use compiler to figure out return type)
- auto func(int i) // {…}
- all returns should resolve to the same type
- in recursive calls, first return must be non-recursive
- Type inference
- by auto:
- auto result = func();
- by decltype:
- int x = 1; decltype(x) y = 2;
- by auto:
- const int i = 1 is not equal to #define i 1
- int *j; later *j is called “dereference”
- C++ coding style: https://google.github.io/styleguide/cppguide.html
- Better only use static_cast (according to Ira Pohl)
- binary literal to string:
std::string str = std::bitset<8>(123).to_string();</code>
- measure execution time of function:
https://stackoverflow.com/questions/22387586/measuring-execution-time-of-a-function-in-c