3.4 KiB
3.4 KiB
C++ Pointers and References — Syllabus
Overview
Master C++ indirection mechanisms from declaration syntax through move semantics. Emphasis on building correct mental models, real-world usage patterns, and surviving interview questions about pointers and references.
Prerequisites
- Basic C++ syntax (variables, functions, structs/classes)
- Understanding of stack vs heap (conceptual)
Learning Objectives
By completion you will be able to:
- Read and write any pointer/reference declaration confidently
- Explain the difference between
&/*in declarations vs expressions - Use const-correctness with pointers and references correctly
- Understand ownership semantics and choose the right smart pointer
- Handle move semantics and forwarding references
- Answer common interview questions on pointers and references
Phase 1: Foundations — Declaration Syntax & Mental Models
&and*in declarations vs expressions — the two contexts- Does
*/&bind to the type or the variable? (C++ parsing rules) - Multiple declarations on one line:
int *a, b— what is b? - Pointers: what they are (address), what they hold, how to dereference
- References: what they are (alias), how they differ from pointers
- Pointer vs reference: when to use which (guidelines)
- Null pointers: NULL vs nullptr vs 0
Teaching Milestone: Explain the "declaration follows use" principle to a beginner.
Phase 2: const Correctness & Function Signatures
const int*vsint* constvsconst int* constconst int&— why it's the default parameter style- Top-level const vs low-level const
- East const vs West const style
- Passing by value vs by reference vs by pointer — tradeoffs
- Return by reference: when safe, when dangerous
- Pointer arithmetic basics
Teaching Milestone: Read a complex function signature and explain what each parameter accepts and whether it can be modified.
Phase 3: Ownership & Smart Pointers
- Raw pointer ownership problems (leaks, double free, dangling)
- RAII principle
std::unique_ptr— exclusive ownershipstd::shared_ptr— shared ownership and reference countingstd::weak_ptr— breaking cycles- Choosing the right smart pointer (decision tree)
std::make_unique/std::make_shared— why prefer them
Teaching Milestone: Refactor a raw-pointer design to use smart pointers and explain your choices.
Phase 4: Move Semantics & Advanced References
- Lvalue vs rvalue — what are they really
- Lvalue references (
T&) vs rvalue references (T&&) std::move— what it does and doesn't do- Move constructors and move assignment
- Reference collapsing rules
- Forwarding references (
T&&in templates) std::forward— perfect forwarding- Common interview traps and trick questions
Teaching Milestone: Explain why std::move doesn't actually move anything.
Resources
- cppreference.com — authoritative reference
- "Effective Modern C++" by Scott Meyers — Items 1-6, 23-26
- Herb Sutter's GotW articles on const correctness
Success Criteria
- Can read any declaration with
*,&,constand explain it - Can choose between pointer, reference, and smart pointer for any use case
- Can explain move semantics without hand-waving
- Can spot pointer/reference bugs in code review