YouTube

Session 18 of Software Lessons, “Programming in C++: Pass by Reference, Pointer and Value Part 1,” runs about 28 minutes.

What this session covers

  • Pass by reference, pass by pointer and pass by value
  • Legal and illegal variants of each
  • The reasoning behind the rules

Key ideas

  • Pass by value gives the function its own copy, so changes inside the function do not affect the caller.
  • Pass by reference gives the function another name for the caller’s object, so changes are visible to the caller and no copy is made.
  • Pass by pointer passes the address of the object, and the function must dereference it; a pointer can also be null.
  • A const reference lets a function read a large object without copying it or changing it.
  • Some combinations are not allowed, for example binding a non-const reference to a temporary value, and the rules exist to prevent accidental changes and dangling references.