const and composite types in C++

  • 2020-05-09 18:54:56
  • OfStack

1. const and references

You can bind a reference to a constant and call it a reference to a constant. That reference cannot be assigned a value.

Such as:


const int ci = 1024;
const int &ri = ci;

Interpretation: ri is a reference to ci. const for ri means take ci as a variable.

For constants, only "references to constants" can be used. int &ri = ci; Is incorrect because ci cannot be assigned, but it may be assigned to ri and affect the const qualification.

So, they created a way to reference constants.

High energy below!!
The above said.

Consider ci as a variable

Why is that?

2. The way to refer to constants refers to variables


int i =2048;
const &ri = i;

Interpretation: ri treats i as a variable and then references it

Effect: you cannot assign ri, but you can assign i. So i is the variable.

So: "references to constants" is a way to refer to |, |! For real constants, you must use this approach, and for variables, this approach prevents the code from assigning values by reference.

Think of it this way: if you want to open an object, someone else can only read it, but you can change the value of that object.


int i;  // Your object 
const int &ri = i;  // Pass this around 

Constant references can be bound to: constant, variable... Literal, expression!!


int i = 5;
const int &ri1 = i; //const int&  Bound to the  int  variable 
const int &ri2 = 9; // Correct: references to constants are ok 
const int &ri3 = r1 * 2; // True: anyway ri Is a reference to a constant 
int &r4 = r1 * 2; // Error: accept your fate 

r3 binds the result of the evaluation of this expression at that time, which is a temporary quantity.

That's the end of quotes and const.

Nightmare, compound and 1 pointer

3. Pointers and const

Good news: Pointers and references are similar.

So:

A pointer to a constant


const int i = 2;
const int *pci = &i;

Just like 1, for constants, you have to use Pointers to constants.

Now point to the variable:


int ii = 2;
const int *pci = ⅈ

Again, you cannot assign to pci after it is dereferenced, but you can assign to ii directly.

Here comes the dish!!

4. const pointer

Review: Pointers are objects, not references.

const pointer means that the pointer object itself is a constant, allowing the pointer itself to be defined as an object.

Effect: the const pointer cannot change the address to the object.

Human words: 1. Must be initialized 2. Can only point to 1.

Putting * before the const keyword means that the pointer itself is constant.


int i = 0; // It doesn't matter whether you're referring to a variable or a constant 
int *const cpi = &i; // will 1 Aimed at  i ;

Big recruit:

Pointers to constants are separated from constant Pointers.

const int *const cpci = &i;

Analysis: a constant pointer to a constant.

Properties: 1. Must be initialized (properties from constant Pointers)

2. The point will not change (property from constant pointer)

3. Can refer to either constants or variables (properties from Pointers to constants)

Free 5.

The above definition is too vague. Cool, that's why C++ is hard.

Definition:

Take Pointers as an example

Top level const: indicates that the pointer itself is constant

Underlying const: indicates that the object to which the pointer points is a constant (or is treated as a constant)

Reasoning:

1. References have no top level const, references are not objects, just bindings.

2. Constants of simple basic types are top-level.

3. Pointers can be both top-level const and low-level const

Pit: don't neglect the bottom const


int i;
const int *pc = &i;
int * p = pc; // Error, pc With the underlying const

Example of using the underlying const: read-only


int i;
const int *ci = &i; // Pass this out, read only. 

Personal opinion on how to understand the definition:

const int                                               |       *const p;

The basic data type pointing to the object is the |       declarant

                    | means that the pointer itself is constant

Bottom const             ci ci ci ci ci ci           | The top const


Related articles: