Some details about C ++ [5: Function - Intermediate]

xiaoxiao2021-03-05  22

8.1 Inline function

* The usual practice is to omit the prototype, put the entire definition (function head function code) in the place where the prototype should be provided.

* Inline functions cannot be recursive. It is not too large, generally only 1, 2 line code.

* Compare with macro:

The macro is achieved by the text replacement. There is a drawback.

The inline function is where the function is replaced with a function, omit the function call. // In the memory.

8.2 Reference variables.

* Reference is an alias of the defined variable. And the original variable points to the same memory address.

* Main purposes: Used as a function of the function, the function will use raw data, not its copy, the same as the pointer. Mainly reference structure and class object parameters.

* Declare the reference to initialize, reference closer closer, once it is associated with a variable, it has been "loyal" in it.

And you can't want to pointers: first declare, then assign the value.

8.3 The referenced reference should be declared as constable as much as possible.

* You can avoid programming errors in unintentional data.

** Use const to make functions to process const and non-constant arguments, otherwise it will only accept non-Const data.

* Using const references, enable functions to generate and use temporary variables: If the argument does not match, its behavior is similar to the value transfer, to ensure that the original data is not modified, and the temporary variable will be used to save the value.

8.4 temporary variable

* If the function of accepting the reference parameters is intended to modify the variable, create a temporary variable will prevent this intent.

* Creating condition 1: The type of argument is correct, but not left value.

Left value parameters are data objects that can be referenced, such as variables, array elements, structural members, reference, and released pointers (* p).

Non-left values: literal constants and expressions containing multiple items.

* Conditions 2: The type of argument is incorrect, but can be converted to the correct type.

8.5 When the function returns a reference or pointer to the data object, it is best to continue in the end of the function.

* You can return the function to the reference or pointer passed to it,

Typea a, b;

......

Typea & Fun (Typea & AA);

Fun (a) = b; // is equivalent to: fun (a); a = B;

Returns the referenced function (name) is actually an alias of the referenced variable, pointing to the memory address of the referenced variable.

* Another method: use new memory to store,

Typet & Clone (Typet & T) {

Typet * pt = new type;

* pt = T;

Return * pt;} // Return reference, point to the new memory address.

8.6 Function Pass - Reference variable Summary:

8.6.1 Do not modify the variable:

* If the data is small, such as built-in data type or small structure,

* If the data object is an array, use a pointer, this is the only choice, the pointer declares to point to the constet.

* If the data object is a bigger structure, use a const reference or const pointer.

* If the data object is a class object, use const references, the reference delivery is the label type that passes the class object parameters.

8.6.2 Modify Variables:

Built-in data types use pointers or references; arrays can only use pointers; structural use references or pointers; class objects use references,

8.7 Default function

* In the prototype setting: The default parameter value is the initialization value.

* The default value must be added from right to left.

* The first parameter is assigned from the order from the left to the right to the corresponding ginseng, and no parameters cannot be skipped.

8.8 function overload (polymorphism)

* Function feature: Function parameter list, parameter arrangement order is equal, function feature is equal. The function characteristics can be overloaded.

* The reference and type of type itself is regarded as the same feature. // is the same as the compiler angle.

* Nor does it distuely distinguish between CONST and non-Const variables. / Illegally illegally illegally illegally illegal.

8.8 Name Modification: C Use it to track each overload function.

8.9 Function Template [...... Renewed]

转载请注明原文地址:https://www.9cbs.com/read-32452.html

New Post(0)