C++ - References

Overview

Estimated time: 30–40 minutes

Bind names to existing objects with references. Pass by reference to modify callers' variables.

Learning Objectives

  • Create lvalue references and const references.
  • Use references for parameter passing and return types safely.

Examples

void inc(int& x){ ++x; }
int main(){ int a=1; inc(a); }

Common Pitfalls

  • Returning references to local variables (dangling reference).