C + + value passing, pointer passing, reference passing in a detailed

Source: Internet
Author: User

Write in front:

This article excerpt the word blog: http://www.cnblogs.com/yanlingyin/archive/2011/12/07/2278961.html

Own understanding of:

The difference between a pointer and a reference is:

The pointer is a copy of the address, although its use can be changed by the address of the value of the argument,

But if you make a change to the copy of the pointer, the change will not affect the argument.

A reference is an indirect operation in which all changes to a formal parameter are mapped to an actual parameter through an indirect operation, and the reference itself is an alias.

So the operation of this alias is the operation of the actual argument.

There are some other differences, that is, references, cannot be empty, must be initialized, sizeof is different, and so on.

There is also a paragraph, not practiced, not quite understood, as follows:

Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. For pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function. If you want to change the related variable in the key function by passing the pointer parameter, you have to use a pointer to the pointer, or a pointer reference.

Finally, if you want to change the correlation variable in the key function by passing the pointer parameter, you use a pointer to the pointer, or a reference to the pointer.

Not too understanding of this sentence.

Awaiting further study.

Value passing:

The parameter is a copy of the argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the called function, the value pass is one-way (argument-and-parameter), the value of the argument can only be passed in,

cannot be outgoing. The value is passed when the parameter needs to be modified inside the function and you do not want the change to affect the caller.

Pointer passing:

The parameter is a pointer to the address of the actual parameter, which is equivalent to the operation of the argument itself when it points to the parameter

Reference delivery:

The formal parameter is equivalent to the "alias" of the argument, the operation of the formal parameter is actually the operation of the argument, in the process of reference passing, the parameters of the function are also used as local variables in the stack

The memory space is opened, but the address of the argument variable that is put in by the main function is stored. Any operation of a modulated function on a formal parameter is handled as an indirect addressing, i.e. by

The address stored in the stack accesses the argument variable in the keynote function. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

1#include <iostream>2 using namespacestd;3 //Value Passing4  voidChange1 (intN) {5cout<<"value passing--function action address"<<&n<<endl;//The copied address is displayed instead of the source address6n++;7 }8 9 //Reference DeliveryTen voidChange2 (int&N) { Onecout<<"reference Passing--function action address"<<&n<<Endl; An++; - } -  //Pointer passing the voidChange3 (int*N) { -cout<<"pointer passing--function operation address"<<n<<Endl; -*n=*n+1; -  }  + intMain () { -     intn=Ten; +cout<<"address of the actual parameter"<<&n<<Endl; A change1 (n); atcout<<"After change1 () n="<<n<<Endl; - change2 (n); -cout<<"After change2 () n="<<n<<Endl; -Change3 (&n); -cout<<"After change3 () n="<<n<<Endl; -     return true; in}

Rules to refer to:
(1) The reference is created and must be initialized (the pointer can be initialized at any time).

(2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).
(3) Once a reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).

The essence of pointer passing:

A pointer pass parameter is essentially the way a value is passed, which is passed an address value. In the process of value passing, the formal parameters of the called function are treated as local variables of the modulated function,

That is, the memory space is opened in the stack to hold the value of the argument that is put in by the main function, thus becoming a copy of the argument. The characteristic of the value transfer is the function of the parameter

Any operation is performed as a local variable without affecting the value of the argument variable of the keynote function. (Here is the address value of the argument pointer itself will not change) if you can't understand it, you can skip the paragraph.

pointer passing and reference passing generally apply to :

The function internally modifies the parameters and wants the changes to affect the caller. Contrast pointer/reference passing can alter the change from the formal parameter "to" the argument (which is actually modified directly in the memory of the argument,

It is not modified to copy the value of the argument to another memory address as if the value was passed.

Another usage is that when a function actually needs to return multiple values, but only explicitly returns a value, you can pass the variable that needs to be returned as a pointer/reference

To the function, so that after the function is modified and returned, the caller can get the modified variable, also equivalent to an implicit return value of the pass bar.

Reference passing and pointer passing are different, although they are a local variable on the function stack space, but any handling of reference parameters is handled by an indirection to the relevant variables in the key function. For pointers passing parameters, if you change the address of the pointer in the called function, it will not affect the relevant variable of the key function. If you want to change the related variable in the key function by passing the pointer parameter, you have to use a pointer to the pointer, or a pointer reference.

Finally, summarize the same points and different points of pointers and references:

★ Same point:

Are the concept of addresses;

The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of a block of memory.

★ Different points:

The pointer is an entity, and the reference is only an individual name;

References can only be initialized once at the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";

The reference does not have a const, the pointer has a const,const pointer is immutable, (specifically, there is no int& const a This form, and the const int& A is some, the former guideline with itself that alias can not be changed, which is of course, so do not need this form, the latter The guideline cannot be changed by the value of the reference)

The reference cannot be null, the pointer can be empty;

The "sizeof reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself;

Pointer and reference self-increment (+ +) operation has different meanings;

References are type-safe, while pointers are not (references are more type-checked than pointers)

C + + value passing, pointer passing, reference passing in a detailed

Related Article

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.