Saturday, December 6, 2025

C++ Reference

A reference is an alias to another variable.

int b = 1;
int &a = b;
a = 2

b will have a value of 2

If & is used to the left side of "=", it is used to represent the address of variable.

A constant reference means one cannot use the reference to change the value that it points to

const int &a  = b
a = 2 generates an error




No comments: