C + + references and pointers

Source: Internet
Author: User

Two reasons to use a reference:

1. The programmer can modify the data object in the calling function.

2, by passing the reference without passing the entire data object, you can improve the speed of the program.


For functions that use passed values without modification:

  1. If the data object is small, you can pass the value directly.

  2. If the data object is an array, use the pointer, because this is the only choice, and the pointer is declared to be a const-only pointer.

  3. If the data object is a large structure, use a const pointer or a const reference to improve program efficiency. This saves time and space needed to replicate the structure.

  4. If the data object is a class object, a const reference is used. The semantics of class design often require the use of references.


For functions that modify the data in the calling function:

  1. If the data object is a built-in data type, the pointer is used.

  2. If the data object is an array, you can only use pointers.

  3. If the data object is a struct, the reference or pointer is used.

  4. If the data object is a class object, the reference is used.

This article is from the "Boundless" blog, please make sure to keep this source http://7492110.blog.51cto.com/7482110/1599191

C + + references and pointers

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.