- Once a reference is declared, the reference name can only be used as an alias of the target variable name, so the reference name can no longer be aliased to another variable name, and any assignment to that reference is the assignment of the target variable name corresponding to the reference.
- To address a reference is to address the target variable.
- Because the pointer variable is also a variable, you can also declare a reference to a pointer variable. The method is:
Type identifier & Reference name = pointer variable name;
- You cannot create a reference to an array because the array is a collection of several elements, so you cannot establish a reference to an array.
- A reference is a reference to a variable or target object, which is not itself a data type, so the reference itself does not account for the storage unit, so that the referenced reference cannot be declared and the referenced pointer cannot be defined.
- Cannot create a reference to a null pointer, such as: Cannot create int &rp=NULL;
- It is also not possible to establish a reference to void type void, such as: &ra=3 cannot be established, because although there is a void data type in the C + + language, none of the variables or constants belong to the void type. It is therefore not possible to establish its reference, and the reference is a reference to a target variable, constant, or object, rather than a reference to a type.
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
Example: Define a reference to a pointer variable and how to use it.
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6 int*A;7 int*&p=A;8 intb=Ten;9p=&b; Tencout << *a <<Endl; Onecout << *p <<Endl; A return 0; - } - the //Rookie, please criticize advice, code writing habits and norms and so on!!! Thank you!!!
I self-study, the textbook may be a bit old, there are problems to ask you correct!!! Thank you!!!
Use of references to C + + references