Name space
Mainly to solve the problem of name conflict
Reference
Aliases for variables
The difference between a reference and a pointer:
1. The pointer is a variable, you can assign it to another address
2. The reference must be initialized at the time of creation and will never be associated with a different variable. The pointer can be assigned to null at the beginning and can then be changed to point elsewhere.
Since pointers are also variables, there can be references to pointer variables, such as the following code that looks a bit weird
int* a = NULL; int*& RA = A; // The reference Ra that represents int* is initialized to a int 9 ; = &b; // Ok,ra is an alias of A and is a pointer
- Void references are not legal
void& A = 3;void is only syntactically equivalent to a type, not essentially a type, without any one variable or the type of reference is void
- Cannot create a referenced array
int a[] ={0}; int& ra[] = A; // Error
- No references to pointers and references
int A; int& ra = A; int& *p = &ra; // error start defines a pointer to a reference
- There is a null pointer, but there is no null reference
Parameter passing by reference
- Passing a reference to a function is the same as passing a pointer. Can change the value of a variable.
- Using a reference as a parameter has a clearer syntax than using pointers
- Use references as parameters \ Return values to give meaning to a function
1. function can only return one value, what if you want to return two values? You can pass two arguments to a function by reference, and then the function fills the target with the correct value
2. Returns a copy of a value when the function returns a value and does not require a copy to be returned with a reference, resulting in increased efficiency
Note: If you return a variable or reference that is not scoped, there is a problem. This is as serious as returning a local-scope pointer, such as
C + + Basics