LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

How do you declare a reference in C++?

Type& name = variable; creates an alias — a second name for an existing variable.

A reference is the same object under another name; a pointer holds the object's address

* A reference is the object under another name (must be bound at birth, never reseated, never null); a pointer merely holds its address. *

int x = 10;
int& ref = x;    // ref is now another name for x

ref = 20;        // changes x to 20
cout << x;       // prints 20

Key differences from pointers:

Reference Pointer
Must be initialized at declaration Can be nullptr
Can't be reseated to another variable Can point elsewhere
Used like the value itself Needs * to dereference
ref = 5 changes the referent *ptr = 5 changes the pointee

Common use — pass by reference:

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

Tip: A reference is like a pointer that's always valid and auto-dereferenced — cleaner to use, but less flexible (you can't reseat it or make it null).

Go deeper:

From Quiz: REVE1 / C++ Programming | Updated: Jul 14, 2026