Focus:
1. Each time the function is called, its formal parameters are recreated and the parameters are initialized with the passed-in arguments.
Note: the mechanism of formal parameter initialization is the same as the initialization of variables.
2. The type of the formal parameter determines how the formal parameter interacts with the argument.
(References , bindings, non-reference, copy)
3. Arguments are: passed by reference (the reference parameter is an alias of the argument) and is passed by value (the argument is two independent objects).
4. Pass-through parameters: all actions done by a function on a formal parameter do not affect the argument.
5. Pointer parameter: The pointer behaves like other non-reference types, and when a pointer copy operation is performed, the value of the pointer is copied. After copying, two pointers are different pointers.
NOTE:C programmers often use pointer-type parameters to access objects outside of the function,and inC + + It is recommended to use a reference type's formal parameter instead of a pointer.
6. The referenced operation actually works on the object referenced by the reference.
7. Some class types do not support copy operations, and functions can only access objects of that type through reference parameters.
8. If the function does not need to change the value of the reference parameter, it is best to declare it as a constant reference.
For example, write a function that compares the length of two string objects, because string objects can be very long, so try to avoid copying them directly. The comparison length does not have to change the contents of the string object, so the formal parameter is defined as a reference to the constant.
compares The length of two string objects
Bool isshorter (const string &s1,const strig &s2)
{
Return S1.size () <s2.size ();
}
Note: If a function does not need to change the value of a reference parameter, it is best to declare it as a constant reference.
9. use formal parameters to return additional information.
When the initial taxiing with the argument is ignored, the top-level constis omitted. In other words: When a formal parameter has a top-level const , it is possible to pass it a constant object or a very mass object.
void FCN (const int i) {/*FCN can read i, but cannot modify i*/}
void FCN (int i) {///error: FCN (int i)} was repeatedly defined
11.c++ allows functions with the same name, but only if the formal parameter lists of the different functions differ significantly, and the top-level const is ignored, so there is no top-level const is the same.
12. You can initialize an underlying const object with a very low volume , but not in reverse!
int i = 42;
Const int *CP = &i; correct, but CP cannot change i;
Int *p = CP; error, type mismatch;
Const int &r = i; correct, but R cannot change i;
Int &R3 = r; error, type mismatch;
Const int &r2 = 42; correct;
Int &R4 = 42; error, cannot initialize a very volume reference with literal value;
13.① to invoke the reference version of reset(Reset (i)), only objects of type int can be used . You cannot use literals, an expression that evaluates to an int , an object that needs to be converted, or a const int type object.
② you want to invoke the pointer version of reset (reset (&i)) can only use int*.
14. Use constant references as much as possible:? The normal reference function can modify the value of its arguments;
? Ordinary references greatly limit the types of arguments that a function can accept.
(for example, integers can only be int, not const objects, literal values, objects that require type conversions)
15. Two properties of the array:
⑴ does not allow copying of arrays;
(therefore, array parameters cannot be used in the way value is passed)
⑵ uses arrays to convert them into pointers;
(So when you pass an array to a function, you actually pass a pointer to the first element of the array)
16. Although it is not possible to pass an array in a value-passing way, we can write the formal parameter as an array-like form: (The following three forms are equivalent)
Void print (const int*);
Void print (const int[]);
Void print (const int[10]);
17. Manage pointer parameters:
① Specifies the length of the array using tags;
Requires that the array itself contains a closing tag;
Void print (const char *CP)
{
IF (CP)//pointer is not a NULL pointer
while (*CP)//pointer refers to a character that is not a null character
Cout << *CP << Endl;
}
② use standard library specifications;
Passes a pointer to the first element of the array and the trailing element;
Void print (const int *beg, const int *end)
{
Outputs all elements between beg and end (without end);
while (beg! = end)
Cout << *beg++ << Endl;
}
③ displays a formal parameter representing the size of the array;
Void print (const int IA [],size_t size)
{
for (size_t int i=0; I! = size; i++)
{
Cout << Ia[i] << Endl;
}
}
/* Three print functions define an array parameter as a pointer to a const, and only when the function does change the value of the element can the parameter definition be exponentially to a very high amount of pointer */
18. Array reference parameters:
&arr the brackets on both ends are not limited
F (int &arr[10])//error: Declares arr as an array of references;
F (int (&arr) [10])//correct: arr is a reference to an integer array with 10 integers
19. The so-called multidimensional array is an array of arrays.
20. A multidimensional array is passed to a function that really passes a pointer to the first element of the array. Because it is an array of arrays, the first element itself is an array, and the pointer is a pointer to an array
Void Print (int (*matrix) [ten], int rowsize);
Void print (int matrix[][10], int rowsize);
size_t Type :
Size _t in order to enhance the portability of the program, there are size_t , on different systems, the definition size_t may not be the same.
The test results show that the size_t is 4 bytes in the system, and in the system of four bits, size_t is 8 bytes, so this type can be used to increase the portability of the program.
definition of size_t
It's defined in /usr/include/linux/types.h
typedef _kernel_size_t SIZE_T;
21.main: Handling command-line options
int main (int argc,char *argv[]) {...}
ARGC: represents the number of strings in an array,argv is an array whose elements are pointers to C -style strings. so : equivalent to
int main (int argc,char **argv) {...}
Note: When using an argument in argv, be sure to remember that the optional argument startswith argv[1] , argv[0] Save the program's name , rather than user input.
22. Functions that handle different number of arguments:
① If all arguments are of the same type, you can pass a standard library type named Initializer_list;
② If the arguments are of different types, we can write a special function, the so-called mutable parameter template.
23.initializer_list is a template type, similar to a vector:
The element type of initializer_list<string> ls;//initializer_list is string
The element type of initializer_list<int> li;//initializer_list is int
(unlike vectors, the elements of a Initializer_list object are always constant values and cannot be changed.) )
Parameter passing: pass-through parameters, pointer parameters, reference arguments, const parameters and arguments, array parameters, main: Handling command-line options, functions with deformable parameters