Catalogue
2 reference as function parameter
3 reference function return value
preface
Through summarizing the basic knowledge of C + +, the blogger is expected to write a simple C + + basic tutorial column and share it with you for reading. In the future, I will continue to update the C + + introductory series of blog posts. Friends who want to learn C + + can pay attention to me and hope you can gain something.
1, const qualifier
(1) Const int * P = & a constant pointer: the pointer can be modified, but the value pointed to by the pointer cannot be changed.
(2) Int * const P = & a pointer constant: the pointer cannot be modified, but the value pointed to by the pointer can be changed.
(3) Const int * const P = & A; modifies both pointers and constants: neither the pointer nor the value pointed to by the pointer can be changed.
int a = 10, b = 20; // 1. const modifier pointer constant pointer: the pointer can be modified, but the value pointed to by the pointer cannot be changed const int * p1 = &a; p1 = &b; // correct *p1 = 30; // error // 2. const modifier constant pointer constant: the pointer cannot be modified, but the value pointed to by the pointer can be changed int * const p2 = &a; p2 = &b; // error *p2 = 30; // correct // 3. const modifies pointers and constants. It modifies both pointers and constants: neither the pointer nor the value pointed to by the pointer can be changed const int * const p3 = &a; p3 = &b; // error *p3 = 30; // error
Memory method:
(1) Translate const into constant and * into pointer in order to distinguish between constant pointer and pointer constant.
(2) When const modifies the pointer, * p is dereference. Const actually defines the object pointed to by the pointer. The pointer itself is not limited, so the pointer can be changed, but the object pointed to is equivalent to a constant and cannot be changed.
(3) When const modifies a constant, * in front of the const qualifier, const p defines the pointer p. at this time, the value of p cannot be changed, that is, the direction of pointer p is constant, while the object pointed to by pointer p is not limited, and the value of this object can be changed.
2, Quote
1 definition of reference
Syntax: data type & alias = original name
Function: alias variables
int a = 10; int &b = a;
Notes: (1) references must be initialized; (2) Once the reference is initialized, it cannot be changed.
Generally, when initializing variables, the initial value will be copied to the new object. However, when defining a reference, the program binds the reference to its initial value, rather than copying the initial value to the reference. Once initialization is completed, the reference will always be bound with its initial value object, and the bound object cannot be changed.
Example 1 int a = 10; // 1. References must be initialized int &b; // Error, must initialize int &b = a; // 2. The bound object cannot be changed after the reference is initialized int c = 20; &b = c; // error b = c; // Assignment operation instead of changing reference cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl;
The values of the above code outputs a, b and c are all 20.
2 reference as function parameter
Function: when passing a function parameter, you can use the reference technology to make the formal parameter modify the argument.
// Example 2: reference transfer and data exchange void mySwap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int a = 10, b = 20; mySwap(a, b); cout << "a= " << a << endl; cout << "b= " << b << endl; system("pause"); return 0; }
3 reference function return value
Usage: function calls can be used as lvalues.
Note: do not return references to local variables.
Example 3 // 1. Do not return a reference to a local variable int& test01() { int a = 10; // Local variables are stored in the stack area in four areas return a; } // 2. Function can be called as an lvalue int& test02() { static int a = 10; // Static variables are stored in the global area, and the data on the global area is released by the system after the program is completed return a; } int main() { int &ref1 = test01(); cout << "ref1 = " << ref1 << endl; // The first result is correct because the compiler makes a reservation cout << "ref1 = " << ref1 << endl; // The second result is wrong because the memory of a has been released int &ref2 = test02(); cout << "ref2= " << ref2 << endl; cout << "ref2= " << ref2 << endl; test02() = 1000; // If the return value of a function is a reference, the function call can be used as an lvalue cout << "ref2= " << ref2 << endl; cout << "ref2= " << ref2 << endl; system("pause"); return 0; }
4 nature of reference
The essence of reference is a pointer constant implemented in C + + (the pointer cannot be modified, but the value pointed to by the pointer can be changed).
int a=10; int &ref=a; // The compiler automatically converts to int * const ref = & A; ref=20; // It is found internally that ref is a reference, which is automatically converted to * ref=20;
5 constant reference
Function: modify formal parameters to prevent misoperation.
int &ref = 10; // Error, the reference must refer to a legal memory space const int &ref = 10; // After adding const, the compiler modifies the code: int temp = 10; const int &ref = temp; ref = 20; // Error. After adding const, it becomes read-only and cannot be modified.
Term: a constant reference is a reference to const
C + + programmers often abbreviate the phrase "const reference" to "constant reference". This abbreviation is quite reliable, but only if you always remember that it is an abbreviation.
Strictly speaking, there is no constant reference. Because the reference is not an object, we can't make the reference itself constant. In fact, since the C + + language does not allow the object bound by the reference to be changed at will, it is understood that all references are constants in this sense. Whether the referenced object is constant or non constant can determine the operation it can participate in, but it will not affect the binding relationship between the reference and the object itself.
Key concept: some symbols have multiple meanings
int i = 42; int &r = i; // &Immediately following the type name, it is part of the declaration, and r is a reference int *p; // *Immediately following the type name, it is part of the declaration, and P is a pointer p = &i; // &It appears in the expression and is an address fetching character *p = i; // *Appears in an expression and is a dereference character int &r2 = *p; // &Is part of the declaration, * is a dereference
In a declaration statement, & and * are used to form a composite type; In expressions, their roles are transformed into operators. Although the same symbols appear in different scenes, they have different meanings, so we can treat them as different symbols.
Concluding remarks
Everyone's praise and attention is the biggest motivation of bloggers. All the code files in bloggers' blog posts can be shared with you (except for a small amount of paid resources). If you want to obtain the complete code files in your blog posts, you can download them through C coins or points. Friends without C coins or points can send them to your email after paying attention, praise and commenting on your blog posts, I will send it to you as soon as possible. There will be more sharing behind the blogger. Please pay attention!