When do I use references? And when to use pointers

Source: Internet
Author: User

when do I use references? And when to use pointers

1. The question was raised:

What is the difference between using value passing and reference passing when an object of a class is passed as a real argument?

For example: Datetype examplefun (CString &strfilename,...) And

Datetype Examplefun (CString strfilename,...)

Before we answer, let's take a look at 2 basic concepts: formal parameters and arguments.

The common argument is that formal parameters are the parameter of form, the actual parameter is the real argument.

Verbose: Formal parameters are simply an abstract type description of an argument, just declaring what type of argument a function (method) can accept, and not determining what the actual parameters of the accepted arguments are;

An argument is the specific content (value) of the corresponding formal parameter passed to the function (method), and the initial reference (content) of the formal parameter is determined by the argument. The parameter is released after the function (method) is returned.

2. Parameter transfer mode: Transmit value and transmit the address;

1). Pass the value method, only the copy of the value of the actual parameter is passed to the function (method), the parameter is manipulated within the method, the object is a copy of the argument, the argument can not be affected. After the method is returned, the parameter is disposed and the contents of the argument are not changed;

2). The address method, the addresses of the arguments are passed to the function (method), in the method of the parameter operation is equal to the actual parameters of the same operation, after the end of the method is returned, the parameters are also released, the contents of the argument will be the result of the operation of the parameters.

and the way of communication can be subdivided into: reference passing (pass-by-reference), pointer passing (Pass-by-pointer)

A reference is actually an alias of an object, a reference to an object that passes the address of an object as an argument, not the object itself.

This is how we understand the answer to the previous question: passing a reference avoids a copy of an argument to a parameter, which improves efficiency.

3. About when to use reference parameters?

(1) The main reasons for using reference parameters are:

Programmers can modify the data objects in the calling function

By passing a reference instead of the entire data object, you can increase the speed of the operation

(2) 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.

(3) 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

4. Summary:

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

1. When you need to change the arguments, you can only use pointers.

2. When passing a large structure and "read-only" its elements,

Because large structures are passed by value, it is too inefficient to copy each of its elements.

3. When you need to iterate through an array or frequently refer to its elements, this is more efficient than using the lower elevation.

4. When allocating space dynamically, you must use pointers.

5. When passing an array, you must use a pointer.

6. When a function returns a pointer, such as fopen

7. 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.

Pointers and references often have the same effect, but references tend to be difficult to read because they are used in the same way as formal parameters, but with the characteristics of pointers. Plus, if the data object is a class object, use a const reference. The semantics of class design often require the use of references, which is the main reason for the addition of this feature to C + +, so the standard way to pass a Const class object is to pass by reference. Other times with good pointers

when to use pointers and references (ii)

First, realize that you cannot use a reference to a null value under any circumstances. A reference must always point to some object.

1. If you use a variable and point it to an object, but the variable may not point to any object at some point, you should declare the variable as a pointer

2. If the variable is definitely pointing to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference.

3. There is no reference to null values this fact means that using the referenced code is more efficient than using pointers. Because you do not need to test its legitimacy before using a reference.

Some of the rules cited are as follows:

(1) The reference is created and must be initialized (the pointer can be initialized at any time).

(2) cannot have a null reference, the reference must be associated with a valid storage unit (the pointer can be null).

(3) Once a reference is initialized, the referenced relationship cannot be changed (the pointer can change the object at any time).

When do I use references? And when to use 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.