Const Small summary of C + + (2)
Today, we discuss the relationship between a pointer or a referenced shape participating in a const.
First of all: in C + +, the parameters of the same name function must be clearly distinguished.
For example, a function with the same name as void reset (int &i) and void reset (const int &i) is not compiled.
int i = 0 const int ci = I; string :: Size_type ctr = 0 ;reset ( &i); Reset ( &ci); // error, cannot be initialized with a pointer to a const int object int* reset (i); reset (CI); // error, cannot bind ordinary reference to const object CI on reset (42 ); // error, cannot bind ordinary reference to literal value Reset (CTR); // error, type mismatch, str is unsigned type
We can see that there are many limitations when using a function without constant reference.
There are two drawbacks to using normal references in functions that do not change the parameters:
- The user of a function is misleading: a function can modify the value of its argument.
- Greatly restricts the types of arguments that a function can accept. We cannot pass const objects, literals, or objects that require type conversions to ordinary reference arguments.
We'll get this sorted out today.
Const Small summary of C + + (3)