Differences Between Constant References, Pointers to Constants, and Constant Pointers in C++
Classification Programming Techniques
Let's first understand some precautions about references and pointers.
A reference is not an object.
A reference must be initialized.
A reference can only be bound to an object and cannot be bound to a literal value or the result of some expression.
The type must be strictly matched.
int &a = 10; // Error: The initial value of a reference type must be an object double a = 3.14; int &b = a; // Error: The initial value of the reference type here must be an object of type int
A pointer itself is an object.
The type of a pointer must strictly match the object it points to.
double dval;
double *pd = &dval; // Correct
double *pd2 = pd; // Correct
int *pi = pd; // Error: The type of pointer pi does not match the type of pd
pi = &dval; // Error: Attempting to assign the address of an object of type double to a pointer of type int
There is an exception: the introduction of the const qualifier
Constant Reference
When initializing a constant reference, any expression is allowed as the initial value.
int i = 42;
const int &r1 = i; // Correct: Allows binding a const int & to a regular int object
const int &r2 = 42; // Correct
const int &r3 = r1 * 2; // Correct
int &r4 = r1 * 2; // Error
double dval = 3.14;
const int &ri = dval; // Correct
// Equivalent to
const int temp = dval;
const int &ri = temp;
int i = 42;
int &r1 = i;
const int &r2 = i;
r1 = 0; // Correct
r2 = 0; // Error
Constant Pointer
Definition: Also known as a constant pointer, it can be understood as a pointer to a constant, that is, this is a pointer, but it points to a constant, and this constant is the value of the pointer (address), not the value pointed to by the address.
Key Points:
- The object pointed to by a constant pointer cannot be modified through this pointer, but it can still be modified through the original declaration.
- A constant pointer can be assigned the address of a variable, and it is called a constant pointer because it restricts modifying the value of the variable through this pointer.
- The pointer can also point elsewhere because the pointer itself is just a variable and can point to any address.
Code Form:
int const* p; const int* p;
Pointer to Constant
Definition: Essentially a constant, modified by a pointer. The value of the pointer constant is the pointer itself, and since this value is a constant, it cannot be assigned.
Key Points:
- It is a constant!
- The address saved by the pointer can be changed, but the value pointed to by the pointer cannot be changed.
- The pointer itself is a constant, the address it points to cannot change, but the content corresponding to the address it points to can change.
Code Form:
int* const p;
Constant Pointer to Constant
Definition: A constant pointer to a constant is a constant, and the object it points to is also a constant.
Key Points:
- It is a pointer constant, pointing to a pointer object.
- The pointer object it points to is also a constant, meaning the object it points to cannot change.
Code Form:
const int* const p;
How to distinguish these types? A const with two is definitely a constant pointer to a constant, which is easy to understand. The main point is how to distinguish between a constant pointer and a pointer to a constant:
One way is to look at the order of * and const, for example:
int const* p; //const * means a constant pointer
const int* p; //const * means a constant pointer
int* const p; //* const means a pointer to a constant
Another way is to see which one const is close to, that is, look from right to left, for example:
```cpp int const* p; //const modifies *p, meaning the content of *p