"C++ Primer" reading notes-Chapter 02 parameter passing

Author: Ma Zhifeng
link: https: //zhuanlan.zhihu.com/p/23726515
Source: know almost
copyrighted by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

statement:

  • The content in the article is collected and compiled from "C++ Primer Chinese Edition (5th Edition)", and the copyright belongs to the original book.

  • The original book has more detailed and wonderful interpretations, please buy genuine books for learning.

  • This article is only for learning and communication, any form of reprinting is prohibited

The mechanism of formal parameter initialization is the same as variable initialization

Pass by value and pass by reference

  • If the formal parameter is a reference type, it will be bound to the corresponding actual parameter

  • Otherwise, copy the value of the actual parameter and assign it to the formal parameter

The pointer also follows the above rules. When calling the function, the value of the pointer parameter is copied and assigned to the pointer parameter. They are two pointers, but they point to the same object.

Passing by reference can avoid copying of objects, so it is recommended to use pass by reference as much as possible

If you do not need to change the value of the reference parameter, it is best to use a constant reference, which can accept more argument types than ordinary references

  • Ordinary references only accept objects of the same type as initial values

  • Constant references can be initialized with objects of the same type, expressions, and literals

If the function requires multiple return values, you can use reference parameters to return additional information

const parameters and actual parameters

void fcn( const int i);  

void fcn( int i );

It does not constitute a function overload, because like other initialization procedures, the top-level const of the formal parameters is ignored. In other words, the actual parameter can be constant or non-constant

Array parameter

Arrays have two special properties

  • Does not support copy

  • Array names are usually automatically converted to pointers

Also makes the array parameters special

void print( const int* );  

void print( const int[] );  

void print( const int[10] );

Subjectively, we would think that these three are different definitions, but the latter two will also be automatically converted to const int * for processing

In the call, the actual parameter can be an array name or an integer pointer

Because the formal parameters are only integer pointers, the dimensions of the array are not passed to the function. There are three ways to solve this problem

  • Pass the pointer of the first element and the last element

  • Add a parameter to pass the size of the array

  • Use array reference parameters

Array reference parameter

void print(int (&arr)[10] );

However, this definition also limits us to only passing arrays of dimension 10.

Multidimensional Arrays

As mentioned earlier, a multidimensional array is actually an array of arrays, and the first element is a pointer to the array

void print(int matrix[10][10], int rowSize);

Why pass rowSize in?

We know that arrays are automatically converted into pointers, and this is no exception. The essence of the above definition is

void print(int (*matrix)[10], int rowSize);

Therefore, rowSize is needed to specify the first dimension of the two-dimensional array

The formal parameters of the main function

The main function is the best example of familiarity with array parameters

int main(int argc, char *argv[]) {}

The second parameter is declared as an array of char *, which means that we can pass multiple strings, and the specific number is specified by argc

Can also be defined as

int main( int argc, char **argv ) {}

Guess you like

Origin blog.csdn.net/qq_26751117/article/details/53233451