Three ways to pass C + + functions are: value passing, pointer passing, and reference passing

Source: Internet
Author: User

Three ways to pass C + + functions are: value passing, pointer passing, and reference passing

Value passing:

void Fun (int5// Modify just y in the stack copy x,x is just a copy of y, a temporary space in memory to re-open the value of Y Sent to X, which also increases the time the program runs and reduces the efficiency of the program.  }void main (void) {int0; fun (y); cout< <\"y = \" <<y<<endl;//y = 0; }

Pointer passing:

void Fun (int *x) {5// modifies the memory cell value pointed to by pointer x }void Main (void) {int0; Fun (&y); cout<<<< \"y = \" <<y<<endl;//y = 5; }

Reference delivery:

void Fun (int &5// modified x refers to the object value &x = y; }void main (void) {int0; fun (y); cout<< <<\"y = \" <<y<<endl;//y = 5; }

1. Value passing: There is a process of copying data from the stack to which the function belongs, if the value is passed an object that is a class object or a large structure object, it will take some time and space.

2. Pointer passing: There is also the process of copying data from the stack to which the function belongs, but the copied data is a fixed 4-byte address.

3. Reference delivery: The same process of copying the data as described above, but for addresses, is equivalent to an alias for the address where the data resides.

In efficiency, pointer passing and reference transfer ratios are highly efficient. The general idea is to use reference passing, and the code is logically more compact and clear.

The reference pass function parameter "is a C + + feature and C is not supported.

Example: Data structure with & without &

With & is the reference parameter, it is the address of the transfer, in fact, the participants with the changes in the parameters of the change, without the & parameter is a general parameter, is the value of the transfer, in fact, the parameter will not change with the change. Therefore, the structure changes, and the need to return this change to use the reference parameter, otherwise with the general parameters. Getelem (l,i) just found the value of the element I, the structure of the linear table has not changed any, so the parameter L do not add &. Listinsert (&l,i,e) is a linear table L in the first element to insert a value of E, the structure of the linear table L has changed, the length has increased, so I must add & before L. If not, the new element will not show up, showing the length of l, and still increase the previous value, 1 less than the actual length.

Three ways to pass C + + functions are: value passing, pointer passing, and reference passing

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.