Parameter Pass (reference, pointer, value pass) c++11

Source: Internet
Author: User

In C + +, the function's pass-through method has value passing and address passing. The pass-through address has pointers and reference methods.
In the function arguments, the reason for the address is:
1. Enable the modulated function to change the data object in the keynote function;
2. The address can reduce the data copy, improve the speed of program operation.

So, when to use value delivery, when to use address delivery (pointers and references)?
Here are some of the things you might encounter when writing code

The data object is not altered when the function uses the Parameter object
  1. Suppose that the data object to pass in the function is small, for example, a built-in data type or a small-scale structure, then use the value to pass;
  2. Assuming the data object is an array, use the pointer (which is also your only option). The pointer is added to the const modifier;
  3. Suppose the data object is a large-scale struct, using a const pointer or a const reference;
  4. Assume that the data object is a class object, using a const reference. This is also the standard practice of passing class objects in C + +;
The data object is changed when the function uses the Parameter object
  1. The data object is the built-in data type, using the pointer (func (&VAR) to indicate that Var is to be changed in the function, which is better readable than the invocation form func (VAR) using the reference;
  2. The data object is an array, and the pointer is the only choice;
  3. A data object is a struct, using pointers or references;
  4. The data object is a class object, using References;

Of course, these are just ordinary situations. Special cases are discussed additionally.

Parameter Pass (reference, pointer, value pass) c++11

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.