Pointer vs Reference

  • Pointer can be nullptr, while reference is always bound to an object
  • Pointer can be adjusted to point to different objects at different times
  • For pointer, * and -> are needed to access the underlying object, while reference always refers to the object

Initializer List

  • Does not allow narrowing conversion (e.g. float to int, char to int is ok)
  • Works for array, class objects

Range-for Statement

for (int k : { 1, 3, 5, 6, 9 }) {
    // Numbers need not to be successive
}

Const-ness

  • Mark any function that do not modify member variables as const to avoid errors
  • Const object can only call const member functions, non-const object can call both

e.g. void Date::print() const; is compiled to void Date::print(const Date* this);

e.g. void Date::print(); is compiled to void Date::print(Date* this);

Lambda

Capture

  • [=] capture all local variables by value
  • [&] capture all local variables by reference
  • [variables] specify only the variables to capture
  • global variables are always usable

Return

  • void by default
  • Automatically inferred if there is return
  • Explicitly specified by []() -> type {}