When does C++ use references and pointers?

1. The method of parameter passing: value passing and address passing

(1) The value-passing method is to pass a copy of the value of the actual parameter to the function (method), and operate the formal parameter in the method. The object is the copy of the actual parameter, which cannot affect the actual parameter. After the method returns, , The formal parameter is released and discarded, and the content of the actual parameter will not change;

(2) By addressing mode, the address of the actual parameter is passed to the function (method). Operating the formal parameter in the method is equivalent to performing the same operation on the actual parameter. After the method ends and returns, the formal parameter is also released. The content of the parameter will be the result of the operation on the formal parameter.

The address pass method can be further subdivided into: pass-by-reference, pass-by-pointer
reference is actually the alias of the object , pass the reference of the object, used to take the address of an object as a parameter Passed on , not the object itself . This is the answer to the previous question that we understand: passing by reference avoids a copy of the actual parameter to the formal parameter and improves efficiency.

2. When to use reference parameters

(1) The main reasons for using reference parameters are: the
programmer can modify the data object in the calling function.
By passing the reference instead of the entire data object, the running speed can be improved.

(2) using the value passed to the function without making changes: if the data object is small, such as built-in data objects, the value of press pass
if the data object is an array, a pointer, and pointers are declared as pointers to const
if If the data object is a larger structure, use const pointer or const reference to improve efficiency and save the time and space required to copy the structure.
If the data object is a class object, use const reference. The semantics of class design often requires the use of references. This is the main reason why C++ has added this feature. Therefore, the standard way to pass class objects is to pass by reference.

(3) For functions that modify the data in the calling function:
if the data object is a built-in data type, use a pointer.
If the data object is an array, you can only use pointers.
If the data object is a structure, use a reference or a pointer.
If the data object is a class object, use a reference.

3. Conclusion

In c/c++ programming, when should you use pointers to pass parameters? Summarized as follows:

  1. When you need to change the actual parameters, you can only use pointers.
  2. When passing a large structure and "read-only" its elements,

Because large structures are passed by value, each element needs to be copied, which is too inefficient.

  1. When you need to traverse the array or frequently refer to its elements, this is more efficient than using subscripts.

  2. When allocating space dynamically, pointers must be used.

  3. When passing arrays, pointers must be used.

  4. When the function returns a pointer, such as fopen

  5. In addition, sometimes you need to use a secondary pointer, that 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 memory cannot be allocated successfully, because at this time, for a, the formal parameters have changed, but the actual parameters will not change. They correspond to different memory units. The correct writing should be:

MemAllocate(char **a){

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

}

In this way, the memory can be allocated correctly.

In many cases, pointers and references have the same effect, but references often bring difficulties in reading because they are used in the same way as formal parameters, but have the characteristics of pointers. In addition,
if the data object is a class object, a const reference is used. The semantics of class design often require the use of references. This is the main reason for the new feature of C++. Therefore, the standard way to pass const objects is
to pass by reference. Other times it’s better to use pointers

4. Other

First, realize that references to null values ​​cannot be used under any circumstances. A reference must always point to some object.

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

2. If the variable must point to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as a reference.

3. The fact that there are no references to null values ​​means that code using references is more efficient than using pointers. Because there is no need to test its legitimacy before using a reference.
Some rules of
reference are as follows: (1) The reference must be initialized when it is created (the pointer can be initialized at any time).
(2) There can be no NULL references, and the references must be associated with legal storage units (the pointer can be NULL).
(3) Once the reference is initialized, the relationship of the reference cannot be changed (the pointer can change the object pointed to at any time).

Reference:
https://www.cnblogs.com/jingzhishen/p/5046631.html

Guess you like

Origin blog.csdn.net/weixin_40437821/article/details/110870552