C + + Primer (fourth edition) after class exercise 7.12

Source: Internet
Author: User

question: When do I use pointer parameters? When do I use reference parameters? Explain the pros and cons of both.

Answer: Parameters are passed by reference parameters and passed as pointers, and programmers are able to modify the data objects in the calling function, preventing copying of objects during parameter passing, which can improve the speed of operation.

1. When to use reference and pointer parameters:

(1) For functions that use passed values without modification:

If the data object is small, such as a built-in data object, it is passed by value

If the data object is an array, the pointer is used and the pointer is declared as a pointer to the const

Use a const pointer or a const reference if the data object is a larger structure to increase efficiency and save time and space required to replicate the structure

use a const reference if the data object is a class object. the semantics of class design often require the use of references, which is the main reason why C + + adds this feature, so the standard way to pass class objects is to pass by reference.

(2) For modifying functions that call data in a function:

The pointer is used if the data object is a built-in data type.

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

Use a reference or pointer if the data object is a struct

Use a reference if the data object is a class object

2. Summary:

When is it time to use pointers to pass parameters in C + + programming? Summarized as follows:

(1) When you need to iterate through an array or frequently refer to its elements, this is more efficient than using the lower level.

(2) A pointer must be used when allocating space dynamically.

(3) When passing an array, you must use a pointer.

(4) When the function returns a pointer, such as fopen

(5) In addition, it is sometimes necessary to use a level two pointer, which is a pointer to a pointer, for example:

Memallocate (char *a) {

A= (char *) malloc (sizeof (char));

}

When this function is called for memory allocation, it is found that the inability to allocate memory is unsuccessful because, for a, the parameters change, but the arguments do not change, and they correspond to different memory units. The correct wording should be:

Memallocate (char **a) {

*a= (char *) malloc (sizeof (char));

}

This makes it possible to allocate memory correctly.

3. Differences:
The pointer may (or may not) point to an actual object. Use the pointer to determine that the value is not 0. Instead of referencing what is not needed.

The pointer can be moved. And the reference form is not able. In the process of use, the reference form is simple, but the flexibility is not small, error-prone and flexible pointer, but error prone.

C + + Primer (fourth edition) after class exercise 7.12

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.