Home > Common Problem > body text

What are the 4 main types of reference?

hzc
Release: 2020-06-19 18:07:07
Original
16741 people have browsed it

What are the 4 main types of reference?

What are the four main types of reference?

Reference type A data type represented by an actual value reference of the type (similar to a pointer). If a variable is assigned a reference type, the variable refers to (or "points to") the original value. No copies are created. Reference types include classes, interfaces, delegates, and boxed value types.

"Reference" is a new variable type of C and an important supplement to C. Its function is to give an alias to the variable. If there is a variable a and you want to give it an alias, you can write it like this:

int a;int &b=a;

This shows that b is a "reference" of a, that is, an alias for a. After such a declaration, using a or b has the same effect and both represent the same variable. In the above reference, & is a "reference declarator" and does not represent an address.

Do not understand it as "assigning the value of a to the address of b". Reference type data is stored in the heap of memory, and the memory unit only stores the address of the object in the heap. Declaring a reference does not open up a memory unit. Both b and a represent the same variable unit.

Note: When declaring a reference variable type, it must be initialized at the same time, that is, declare which variable it represents. After declaring a reference to a variable, the reference is always associated with the variable it represents during the execution of this function and cannot be used as an alias for other variables. The following usage is incorrect:

int a1,a2;

int &b=a1;

int &b=a2;//Attempt to make b an alias of a2 (quote ) is not acceptable. This is wrong.

We can assign the value of a2 to b.

b=a2;

The above is the detailed content of What are the 4 main types of reference?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template