When a generic variable is declared, the corresponding memory space is created, which is used to store the value of the variable. When a function is passed by value, a copy of the value of the variable is passed to the function, so changing the value in the function will not change the value of the variable.
Pointer variables and passed by pointer. A variable of pointer type, after declaring, creates a corresponding size of memory space based on the operating system, which stores an address that points to a variable. If a function passes a parameter by pointer, it also passes a copy of the variable, but the variable is a point to the address of a memory unit, the variable is referred to the operation, changing the value of its memory unit stored, you can change the value of the variable dereference.
Referenced and passed by reference. A reference is an alias for a variable. When declaring a reference, no memory space is allocated for the reference, just an alias to the variable. A reference operation is both an operation on a variable, so when a function passes a variable by reference, changing the value of the reference changes the value of the variable.
Void fun1 (int& ntemp);
Void fun2 (const int& ntemp);
FUN1 (100);
FUN2 (100);
function fun1 the parameters are int reference variable of type, which needs to point to a piece of memory space, and - are constants, constants are not allowed to be copied and modified, and do not occupy memory space, so this function call is wrong. (A temporary variable is created when the system passes a parameter, and the temporary variable cannot be a non- const reference parameter)
function parameters are const int const
(the C + + compiler has a syntax restriction, if a parameter is passed in as a non-const reference, then the C + + compiler has reason to believe that our incoming parameter can be modified in the function, and that the modified variable is also available outside the function, However, if we pass in a temporary variable as a non-const reference parameter, the programmer cannot manipulate the temporary variable due to the particularity of the temporary variable, and the temporary variable is freed after the use is complete, so if we modify a temporary variable it actually doesn't make sense, so C + + The compiler does not restrict the semantics of non-const references when it joins temporary variables, mainly to limit the potential errors of this very compliant usage. Temporary variables are actually available as Lvalue, which is described in section 7.3 of the first volume of thinking in C + +. A non-const reference can only point to an object of the same type that is not const, but a const reference may be initialized to an object of a different but related type, or to an rvalue. )
(Temporary variables: in C + +, the temporary variables in the real sense are invisible, meaning they don't appear in your code.) Temporary variables are not variables that we create temporarily, but temporary variables that are automatically generated by the compiler when the program needs them, and they do not appear in the code, but they do exist, and the timing of the temporary variable is usually the type conversion that occurs when the function parameter is passed, and the function returns the value that is created. )
int* pprint = new int;
Void fun3 (int* ptemp) {ptemp= new int ();}
Void fun4 (int* &ptemp) {ptemp = new int ();}
Fun3 ( Pprint );
Fun4 ( Pprint );
function Fun3 get an argument Pprint copy, the content of the variable is an address, and the function body is assigned to a new address, because it is a copy of the argument, so you cannot change the value of the argument, that is, Pprint the value is not changed.
function Fun4 parameter is int reference to the type pointer, which is int the pointer itself, changing the value through the function body, can change the argument Pprint the value.
Returns a reference. If the function returns a local variable, the local variable is destroyed after the function ends, and the reference to a destroyed memory space is not allowed, so a reference to the local variable is wrong.
C + + parameter passing (reference, TEMP variable)