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

Source: Internet
Author: User

Tag: Reference value Pass reference pass

In C + +, the parameters of a function are passed by value, and address is passed. 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 modify the data object in the keynote function;
2. Address can reduce the data copy, improve program execution speed.

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 modified when the function is being tuned using a Parameter object
  1. If the data object to pass in the function is small, such as a built-in data type or a small-scale struct, then the value is passed;
  2. If the data object is an array, use the pointer (which is also your only option). The pointer adds a const modifier;
  3. If the data object is a large-scale struct, use a const pointer or a const reference;
  4. If the data object is a class object, use a const reference. This is also the standard practice of passing class objects in C + +;
The data object is modified when the function uses a Parameter object
  1. The data object is the built-in data type, using the pointer (func (&VAR) to indicate that Var is to be modified 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 general conditions. Special cases are discussed additionally.

Parameter passing (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.