On the way C ++ three kinds of mass participation

On the way C ++ three kinds of mass participation

C ++ parameter passing to functions mainly in three ways: namely, the value transfer, and transfer the pointer reference.

The following three methods will be described by explaining differences and examples.

Value transfer

As we all know, the definition of the parameters in parentheses are the function parameter is a function specific to the local variables, means that the function is received copy argument, if the shape parameter value is changed within the function, to the argument It is not affected .

#include <iostream>

using namespace std;

void change(int formalNum) {
    formalNum = 0;
    cout << "formalNum address: " << &formalNum << endl;
}

int main() {
    int realNum = 10;
    cout << "Before Change: " << realNum << endl;
    cout << "realNum address: " << &realNum << endl;
    change(realNum);
    cout << "After Change: " << realNum ;
    return 0;
}

// 执行结果
Before Change: 10
realNum address: 008FFDA0
formalNum address: 008FFCCC
After Change: 10

It can be seen, arguments and parameters completely different addresses, and no way to change the value of the function argument. The value passed role more internal function is to understand the value of an external parameter. Value transfer is unidirectional, only by the pass parameter arguments.

Pointer is passed

Pass a pointer is well understood that the parameter is a pointer to the address of the argument, when the operation parameter, the operation is equivalent to directly address arguments.

#include <iostream>

using namespace std;

void change(int *ptr) {
    *ptr = 0;
}

int main() {
    int realNum = 10;
    int* ptr = &realNum;
    cout << "Before Change: " << realNum << endl;
    change(ptr);
    cout << "After Change: " << realNum ;
    return 0;
}

// 执行结果
Before Change: 10
After Change: 0

We can clearly see that we have succeeded in modifying the value of the argument within the function. C ++ is a very common way to pass parameters.

Passed by reference

Passed by reference is actually the hardest to understand a way to pass parameters. Prior to the detailed analysis of it, let's say his function.

Pass parameters to a function call by reference method, copy the address reference to the formal parameters. Inside the function, the reference to the actual parameters to use to access the call. This means that modification of parameters affect the actual parameters.

So sure someone asked, as are directly affected, pointers, and references so what difference does it make? ? ? That difference may go big.

  1. Is essentially a pointer variable , the variable is an integer, is the address of another variable. Pointers are logically independent, it can be changed, even change its value (point to other addresses), and may remove the corresponding data memory.
  2. References can be understood as a nickname, is synonymous with another variable, it has logically dependent , so C ++ also provides a reference at the time of creation must be initialized (an existing variable, and then create a reference to the variable) . And the object it references can not be changed throughout its life cycle , that is, from beginning to end can only be attached to the same variable (initialization of representatives who alias, who has been the alias can not be changed).
  3. In reference to the transfer process, the transfer function of the form of local variables as parameters although opened memory space in the stack, but at this time is the address placed by the calling function to the argument variables. Is any operation parameter of modulation functions are treated as indirect addressing, i.e. access the argument in the calling function by the variable address stored in the stack. Because of this, any operation is made on the parameter adjustment function argument variables are influenced in the calling function.

Cited rules:

  • References while being created must be initialized (pointer can be initialized at any time).
  • Can not have a NULL reference, reference must be associated with valid memory cell (pointer may have a wild pointer can point to NULL).
  • Once a reference is initialized, not change the relationship between a reference (pointer may be changed at any time within the meaning of the object).

Read so many, passing a pointer and passed by reference what use is it?

  1. Internal function to modify the parameters and you want the changes affect the calling function. Comparative pointer / reference may be changed to "pass" arguments (actually changes directly on the arguments memory) by a parameter;
  2. When a function of the actual need to return multiple values, but only an explicit return a value, additional variables may be required to return a pointer / reference.

See the following specific actions:

The method defined reference variable and the like conventional variables, but a data type and name between &symbols. For example, the following function is defined so that the parameter refNumbecomes a reference variable:

#include <iostream>

using namespace std;

void change(int& refNum) {
    refNum = 0;
    cout << "reference address: " << &refNum << endl;
}

int main() {
    int realNum = 10;
    cout << "Before Change: " << realNum << endl;
    cout << "realNum address: " << &realNum << endl;
    change(realNum);
    cout << "After Change: " << realNum ;
    return 0;
}

// 执行结果
Before Change: 10
realNum address: 00A4F9F4
reference address: 00A4F9F4
After Change: 0

Can be seen, passed by reference we succeeded in changing the values of the parameters, while parameter address and the address arguments are actually identical .

Guess you like

Origin www.cnblogs.com/scyq/p/12372479.html