C ++ constructs the Data Type ---- reference, constructs the Data Type ----
When it comes to reference, I think of my friends starting external accounts when I was a child. The foreign accounts of my classmates are sometimes very vivid. Since
After someone has a special nickname, his name is banned. After a long time, as long as a student's nickname is called, he agreed.
Knowledge originates from life. It also appears in C ++. It is not as humorous as a human nickname, but elegant as reference. Reference in programming
Very practical and convenient. It is mainly used to describe the parameters and return values of the function, so that the program is concise and efficient.
I. Definition
FormData Type & reference variable name = variable name
For example, the word World Trade Organization (World Trade Organization) is too long. Give it a nickname.
Char & WTO = World Trade Organization
Since then, the World Trade Organization has been called the WTO, but whatever it is called, it refers to the same thing.
Note:
1. Initialization is required when defining a reference.
2. The reference name is unique and cannot be referenced by other variables.
3. Only definitions of existing variables can be referenced.
II. Application
1. As a function parameter
When a function is used, we usually put the real parameter value into the function parameter list. A real parameter is passed to the form parameter. But reference as a function
When the function is used, the parameter in the function is already the value of the referenced variable. This is similar to the pointer as a function parameter.
2. Return Value as Function
Using an example to deeply understand the returned value
Include "stdafx. h "# include" stdlib. h "# include <iostream> using namespace std; int max1 (int a [], int n) // calculate the maximum value in array a [] {int t = 0; for (int I = 0; I <n; I ++) if (a [I]> a [t]) t = I; return a [t];} int & max2 (int a [], int n) {int t = 0; for (int I = 0; I <n; I ++) if (a [I]> a [t]) t = I; return a [t];} int & sum (int a [], int n) {int s = 0; // m4 will reference s to see what is wrong after the reference (int I = 0; I <n; I ++) s + = a [I]; return s;} int main () {int a [10] = {1, 2, 4, 5, 6, 7, 8, 9, 10}; int m1 = Max1 (a, 10); int m2 = max2 (a, 10); // The return type of max2 () is int &, int & m3 = max2 (a, 10); int & m4 = sum (a, 10); // return variable s by m4 reference, because s is a variable in the function body, s becomes invalid when the function call ends. // The final m4 is an invalid value. Therefore, the referenced type variable returned by the function cannot be a temporary variable. Cout <"m1 =" <m1 <endl; cout <"m2 =" <m2 <endl; cout <"m3 =" <m3 <endl; cout <"m4 =" <m4 <endl; system ("pause"); return 0 ;}
3. Frequent reference
Add the const modifier before declaring the referenced data type. In this way, you cannot change the value of the referenced variable through regular references.
Iii. Reference comparison pointer
Commonalities
Both are indirect access variables.
Initialize before use
Differences
References do not occupy new addresses, saving memory
The address of the referenced variable cannot be modified, which is safer to use.
Multi-Use Pointer for low-level programming and multi-use reference for Advanced Programming
Iv. Summary
Referencing is also an important part of programming languages. It simplifies programming and improves data access security. For reference, I now
I don't know much about it. I believe that we will often deal with it in future programming. If any
And look at the axe.