Reference (Code instance) in C ++ to reference an instance
References in C ++:
Reference introduces a synonym for an object. The expression of the definition reference is similar to the definition pointer, but it is replaced *. Reference is an important extension of c ++ to c language. Reference is
An alias of a variable (target). The referenced operation is exactly the same as the direct operation on the variable. The format is: Type & reference variable name = defined variable name.
Features of reference:
① A variable can have multiple aliases.
② The reference must be initialized.
③ The reference can be referenced only once during initialization and cannot be changed to reference other variables.
① Basic reference:
[Cpp] view plaincopy
VoidTestReference1 ()
{
Inta = 1;
Int & B =;
Cout <"a: address->" <& "<& B <
A = 2;
B = 3;
Int & c = B; // reference a referenced variable, alias
C = 4;
}
② Const reference: [cpp] view plaincopy
VoidTestReference2 ()
{
Intd1 = 4;
Constint & d2 = d1;
D1 = 5; // The d1 value changes, and the d2 value also changes.
// D2 = 6; // you cannot assign values to constants (the quantity cannot be modified.
Constintd3 = 1;
Constint & d4 = d3;
// Int & d5 = d3;
Constint & d6 = 5; // constant, which can be referenced only when a constant is referenced.
Doubled7 = 1.1;
// Int & d8 = d7; // d7 is of the double type and d8 is of the int type. When d7 is assigned to d8, a temporary variable is generated.
// That is to say, d8 references this temporary variable with a regular nature, so it cannot be assigned a value.
Constint & d9 = d7;
}
③ Reference as a parameter:
[Cpp] view plaincopy
1. [value transfer] If the parameter is not a reference value transfer method, a local temporary variable is generated to receive the value of the real parameter.
VoidSwap (intleft, intright) // The value transfer method cannot realize the exchange, because when passing parameters left and right copy a temporary copy, the exchange is the copy value, because it is a temporary variable function exit, the variable pin {// is destroyed and does not affect the external left and right values.
Inttemp = left;
Left = right;
Right = temp;
}
2. [Reference transfer] if the form parameter is of the reference type, the form parameter is the alias of the real parameter.
VoidSwap (int & left, int & right) // if a reference is used, no temporary copy is made. & the usage instructions here are just another name of the original parameter, therefore, the variable value is directly modified based on the original parameter.
{
Inttemp = left;
Right = left;
Left = temp;
}
3. [pointer Transmission]
VoidSwap (int * pLeft, int * pRight) // The address is passed in. Because the address is unique, the pointer can access the address to modify its content.
{
Inttemp = * pLeft;
* PLeft = * pRight;
* PRight = temp;
} Exercise caution when referencing easily:
(1) & here it 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, namely, the original name and reference name of the target, and the reference name cannot be used as the alias of other variable names.
(5) find the address of the reference, that is, find the address of the target variable. That is, the reference name is an alias of the target variable name. Reference is defined as a reference that does not occupy any memory space, but the compiler generally
It is implemented as a const pointer, that is, a pointer pointing to an immutable position. Therefore, the reference actually occupies the same memory as the general pointer.
(6) The referenced array cannot be created. Because an array is a collection composed of several elements, you cannot create a collection composed of references, but you can create an array reference.
(7) reference common usage: as a function parameter and return value.
Summary:
1. Do not return a reference to a temporary variable.
2. If the returned object shows that the current function's scope still exists, it is better to use the reference to return, because this is more efficient.
* Differences and relationships between references and pointers (test hotspot)
1. The reference can only be initialized once during definition, and cannot be changed to other variables (from the beginning to the end). The value of the pointer variable is variable.
2. The reference must point to a valid variable. The pointer can be null.
3. sizeof pointer object and referenced object have different meanings. Sizeof references the size of the variable to which the object is directed, while the sizeof pointer is the size of the object address.
4. the pointer and reference auto-increment (++) Auto-Subtract (--) have different meanings.
5. References are relatively safer than pointers.
Pointers are more flexible than references, but they are highly risky. When using pointers, you must check whether the pointer is NULL, and it is best to set the pointer after space collection.
Zero to avoid Memory leakage and other problems caused by wild pointers.
I. Differences and links between references and pointers:
★Differences:
1. the pointer is an entity, and the reference is only an alias;
2. You do not need to unreference (*) when using a reference. The pointer must be unreferenced;
3. The reference can only be initialized once during definition, and will not be changed afterwards; the pointer will be variable;
4. No const is referenced, and the pointer has const; the Pointer Modified by const is unchangeable;
5. The reference cannot be blank, and the pointer can be blank;
6. "sizeof reference" gets the size of the variable (object) to which it points, while "sizeof Pointer" gets the size of the pointer itself (the address of the variable or object to which it points;
7. the pointer and reference auto-increment (++) operations have different meanings;
8. In terms of memory allocation, the program allocates memory areas for pointer variables, but does not need to allocate memory areas for reference.
★Similarities: both are addresses. pointers point to a memory and the content is the address of the memory. References are the alias of a memory.
Ii. const's meaning in C and C ++ (written test hotspot ):
(1) const in C, which has a single function and is easy to understand:
Purpose: The modified content cannot be changed.
Usage scenarios: Modify variables, function parameters, and return values. (Many application scenarios are required in c ++)
Feature: const during runtime, so it cannot be replaced by # define, which is used to become the case of array length and need to be compiled frequently. At the same time, because it is a const at runtime, it can be defined but not initialized, but initialized at runtime. For example, const int iConst ;. In addition, in c, the const variable is an external link by default. Therefore, if there is a const variable with the same name in different compilation units, a name conflict occurs and an error is reported during compilation.
(2) const in c ++:
A. Non-class member const:
① The const variable is internally connected by default. Therefore, different compilation units can have the same name const variable definition.
② It is often used during compilation, so it can be used like # define, and because of the above, you can define the const variable in the header file, including different cpp files (compile
Unit) to avoid naming conflicts.
③ By default, the compiler does not allocate memory for the const variable unless: 1. Use extern to declare that 2: The program has the address to reference the const variable.
④ The temporary object/built-in variable in c ++ has the const attribute by default.
B. const in the class:
(1) Like const in C language, it is only the number of runtime periods and cannot be used as the array dimension, that is, it cannot replace # define. Use the following two methods to replace # define: 1: staticconst...
2: enum {...} // enum does not occupy storage space.
② The const variable in the class occupies the storage space.
③ The const member variables in the class must be initialized in the const initialization list.
④ Const object: During the object lifecycle, no member variables must be changed. The const object can only call the const member function.
⑤ Const member function: void fun () const... can be called not only by the const object, but also by non-const object. Therefore, if you confirm that any member function is not modified
To change any member variable, you should define the function as a const type.
⑥ If an object is defined as const, the const object may be put into ROM, which is sometimes very important in embedded development.