c++ basics--const, reference, pointer

As shown in the code above, add & to the parameters passed in to the function. When calling the function, the address of the variable is passed in. The function does not need to copy the variable and directly modify the original variable at the address. If you only want to read the address Instead of modifying the variable value, you can add a const parameter in front to make the function's operation on the variable read-only.

The following example shows the advantage of passing in an address: (Let me start by saying that both reference and passing in the address will change the value of the variable, so const needs to be added flexibly.)

There are actually many advantages, such as the following. Defining a comparison function can compare different variables.

 There is also such a callback function. Function pointers allow you to pass one function as a parameter to another function. This function can call the function pointer at a later time, a technique called callbacks. This allows you to specify when and how a certain function should be executed.

Guess you like

Origin blog.csdn.net/qq_40962125/article/details/130729175