Value Transfer, reference transfer, pointer Transfer

Source: Internet
Author: User
Recently, it has been plagued by the concept of C ++ value transfer, reference transfer, and pointer transfer. It has never been a simple example or simple statement to clarify the relationship between them, I found some relevant information and my personal feelings. I 'd like to give a brief introduction.
There are three methods for passing parameters in C ++: Passing parameter values (referred to as passing values), passing parameter addresses (referred to as address transfer, or passing parameters for short ), and reference transfer (referred to as transfer reference), the corresponding function is the call to pass the value, transfer the address call and transfer the reference call
When a function is defined, the parameters in the parameter table are called formal parameters. When a function is called, the parameters in the parameter table are called actual parameters, and the parameters in the parameter table are referred to as real parameters, generally, the C ++ language calls value passing, and the value passing call is unidirectional. That is, the value of a parameter can only be passed to the form parameter by the real parameter, but not to the value parameter by the form parameter, that is to say, from the perspective of the called function, the parameter value can only be passed in and cannot be passed out. When you call a value, the real parameter can be a specific value, a variable that already has a value, and an expression that can be evaluated. because both the value and the variable represent the city, when you call a value, the real parameter is essentially an expression. When a function is called, the system first expresses the ball value of the real parameter, and then passes the value to the shape parameter. In another way, when the value is called, the parameter is actually a copy of the real parameter, so the value of the real parameter of the external variable is not changed during transmission,
Suppose we declare a variable int n = 1; then it is stored in the memory. We can understand it as follows: First, memory 1 refers to the value of Variable N, the address that stores this value. We call it the memory address of 1, that is, the pointer address. N is his name, or we call it the name of 1, in addition, we can give him another name, called a nickname or alias. because both the name and the nickname point to the same value, the value of the name is changed, that is, the value of the nickname, the nickname is a reference of the famous name. After clarifying the above meanings, let's give an example.
Int m;
Int & n = m;
N is the reference of M, and m is the referenced object referent. N is equivalent to M alias,
Some referenced rules are as follows:
(1) The reference must be initialized at the same time (the pointer can be initialized at any time ).
(2) there cannot be a null reference, and the reference must be associated with a valid storage unit (the pointer can be null ).
(3) once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time ).
The main function of the reference is to pass the parameters and return values of the function.
In C ++, function parameters and return values are transmitted in three ways: value transfer, pointer transfer, and reference transfer.

The following is an example of "value transfer ".Program.
Since X in the func1 function is a copy of the external variable N, changing the value of X does not affect N, so the value of N is still 0.
# Include <iostream>

Using namespace STD;

Func (int x );

Void main (){
Int n = 0;
Func (N );
Cout <n <Endl;
}

Func (int x ){
X = x + 10;
}

The following is an example program for "pointer passing.
Since X in the func2 function is a pointer to the external variable N, changing the content of this pointer will change the value of N, so the value of N is 10.
# Include <iostream>

Using namespace STD;

Func (int * X );

Void main (){
Int n = 0;
Func (& N );
Cout <n <Endl;
}

Func (int * X ){
* X = * x + 10;
}

The following is a sample program for "reference transfer.
Since X in the func3 function is a reference of the external variable n, x and n are the same thing, changing X is equal to changing N, so the value of N is 10.
# Include <iostream>

Using namespace STD;

Func (Int & X );

Void main (){
Int n = 0;
Func (N );
Cout <n <Endl;
}

Func (Int & X ){
X = x + 10;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.