Values & References10 / 16
Up next

Reference vs pointer

#include <print>

int main() {
    int a = 1;
    int b = 2;
    // a reference binds once and always aliases a
    int& r = a;
    r = b;
    // this copied b's value into a; it did NOT rebind r
    std::println("{} {}", a, b);
    // a pointer can be re-pointed at will
    int* p = &a;
    p = &b;
    std::println("{}", *p);
}
What you're solving

a reference cannot be null or rebound