Many programmers have learned C ++ and concluded that C ++ is an independent language rather than a C language, you can directly learn C ++ without starting with C. Next we will analyze C ++ references ..
Example 1: int a; int & ra = a; // defines the reference ra, which is the C ++ reference of variable a, that is, the alias.
1) & this is not an address calculation, but an identifier.
2) type identifier refers to the type of the target variable.
3) When declaring a reference, it must be initialized at the same time.
4) after the reference declaration is complete, it is equivalent that the target variable name has two names: the original name and reference name of the target, and the reference name cannot be used as the alias of other variable names.
Ra = 1; equivalent to a = 1;
5) declare a reference. Instead of defining a new variable, it only indicates that the reference name is an alias of the target variable name. It is not a data type, therefore, the reference itself does not occupy storage units, and the system does not allocate storage units to the reference. Therefore, finding the address for the reference is to find the address for the target variable. & Ra and &.
6) You cannot create an array reference. An array is a collection composed of several elements, so an array alias cannot be created.
Reference Application
Example 2 ]:
- Void swap (int & p1, int & p2) // The parameter p1 and p2 of the function are referenced.
- {Int p;P=P1;P1=P2;PP2= P ;}
To call this function in a program, the call point of the corresponding main function can be directly called using the variable as the real parameter, without any special requirements on the real variable. For example, the swap function defined above can be written as follows:
- Main ()
- {
- Int a, B;
- Cin>>A>>B; // enter the values of a and B.
- Swap (a, B); // directly call the swap function using variables a and B as real parameters.
- Cout<<A<<''<<B; // Output result
- }
When the above program is running, if the input data is 10 20 and then press enter, the output result is 2010.
From example 2, we can see that:
1) passing a reference to a function has the same effect as passing a pointer. In this case, the parameter of the called function becomes an actual parameter variable or an alias of the object in the original main function, therefore, the operations on the parameters in the called function are the operations on the corresponding target object in the main function.
2) using C ++ to reference the parameter passing function does not generate a copy of the real parameter in the memory. It directly operates on the real parameter, while using a common variable to pass the function parameter, when a function call occurs, you need to allocate storage units to the parameters. The parameters are copies of the real variables. if the object is passed, the copy constructor will also be called. Therefore, when the data transmitted by a parameter is large, it is better to use reference than to transmit a parameter using a common variable.
3) Although using pointers as function parameters can achieve the same effect as using references, you must allocate storage units to the parameters in the called functions, the "* pointer variable name" format must be used repeatedly for calculation, which can easily lead to errors and the program reading is poor. On the other hand, at the call point of the main function, the variable address must be used as the real parameter. References are easier to use and clearer.
If you want
- How to Write C ++ project development and project plan correctly
- Summary Notes on learning and exploring C ++ library functions
- In-depth demonstration of high security of C ++
- Describes in detail how to accurately Write C ++ languages.
- In-depth demonstration of high security of C ++