Tham chiếu là alias của một biến đã tồn tại — cùng địa chỉ, cùng giá trị, khác tên gọi.
cpp
int x = 10;
int& ref = x; // ref là bí danh của x
ref = 20; // x cũng thành 20Khác con trỏ:
| Tham chiếu | Con trỏ | |
|---|---|---|
| Null | Không được null | Có thể null |
| Reassign | Không đổi được target | Có thể |
| Cú pháp | dùng . | dùng -> hoặc * |
| Phép toán | không có arithmetic | có pointer arithmetic |
Hình dung: tham chiếu là "tên khác" của cùng một căn nhà; con trỏ là "địa chỉ" trên bản đồ.
A reference is an alias for an existing variable — same address, same value, different name.
cpp
int x = 10;
int& ref = x; // ref is an alias of x
ref = 20; // x becomes 20 tooDifferences from pointers:
| Reference | Pointer | |
|---|---|---|
| Null | Cannot be null | Can be null |
| Reassign | Always binds to same target | Can be retargeted |
| Syntax | uses . | uses -> or * |
| Arithmetic | none | supports pointer arithmetic |
Mental model: a reference is another name for the same house; a pointer is the street address on a map.