Recently, when I was studying C ++ and reading C ++ prime (Third edition), I wrote down some sporadic knowledge points, mainly for some details, it is easy to look back and consolidate it.
1. Functions
1.1 function parameter transfer:
C ++ function parameters are passed by value, but passing by value is not very suitable in some cases.
1. When a large class object must be passed as a parameter, the allocation object needs to be copied, which requires a large overhead.
2. When the real parameter must be modified
In this case, the pointer or reference must be used as a parameter to pass to the function.
|
Pointer |
Reference |
|
1. When a pointer is used as a function parameter, it can point to a series of different objects, or it can point to nothing.
2. The function cannot be safely unbound before determining whether the pointer points to a valid object.
|
1. When a reference is used as a function parameter, the reference must be initialized to point to an object. Once initialized, it cannot point to other objects.
2. When a reference is used as a function parameter, you do not need to consider whether it points to an object, because the reference must point to an object.
3. References can be used in function overloading for more intuitive purposes.
|
1.1.1 differences between pointer and reference parameter
1.1.2 array parameters
1. The array is passed as a pointer. To avoid modifying the real parameter, you can declare the parameter as a const type.
2. The function does not check the length of the array because the address of the first element is passed but does not contain the length of the passed array.
3. To check the length of the passed array, you can declare the parameter as an array reference. When the parameter is an array reference, the parameter actually contains the length of the array,
4. the compiler checks the length of array parameters.
1.1.3 abstract container type parameters
1. for parameters of the abstract container type, if it is a value transfer, all elements in the container need to be copied, the efficiency is too low.
2. You can consider using pointers or references as parameters. If you do not want real parameters to be modified, you can declare them as the const type.
1.1.4 default real parameters
1. When a function is declared, the default real parameter can be declared. If a parameter is passed, the default parameter at the corresponding position will be overwritten.
2. The real parameters of the function call are parsed by location. The default real parameters can only be used to replace the missing tail real parameters of the function call.
3. Before any default parameter is provided, the rightmost uninitialized parameter must be provided with the default parameter. That is, the default real parameter is specified from right to left.
4. A parameter can only be specified once in a file. Therefore, the default real parameter is usually specified in function declaration.
1.1.5 ellipsis
Sometimes we cannot list the number and type of all real parameters passed to the function. This is a function parameter table that can be replaced by a ellipsis.
1.2 inline functions
1. The expansion of inline functions is determined by the compiler. For recursive functions, they are expanded only when they are called for the first time.
2. inline functions are generally used in the case of frequently called functions with a small scale.
3. If inline functions appear in multiple files, they need to be defined in each file. They can be defined in the header file and included as needed.
1.3 pointer to function
1. function name = & function name
2. pointers to functions can be used without any reference
3. pointer to function does not support type conversion