C + + preliminary learning


C + + main features: compiled language, strong typing (not type safe), operator overloading, generics (STL library container).

Anonymous Union: it mainly reflects the way member variables are distributed in memory. Nameless objects cannot be called by the outside world.

Bool: any primitive type can be implicitly converted to a boolean type.

Function overload:

Principle: G + + compiler name changing mechanism. If the formal parameter table is different, the function name after name changing is different.

Extern C: compiled in the C language, cannot be renamed or function overloaded.


Principle of compiler matching function:






Default parameter: specified by declaration, not specified by definition; A parameter has a default value, which is required for subsequent parameters; Avoid overload ambiguity caused by default parameters.


Inline function: after compilation, replace the function call with binary code. Reduce the overhead of call time (save the scene, out of the stack and into the stack). C + + inline functions are better than C language’s parametric macros (macro functions).

Dynamic memory allocation:

Malloc / free and new / delete: the former continues to be available, while the latter allocates and initializes at the same time (such as class initialization table, reference, etc.).

Array allocation: type * P = New   Type [] {}
Release: delete []   p; p = NULL; (prevent double release error)


The alias of memory, which refers to the operation on the reference, that is, the operation on the target memory; It does not occupy memory and is not an entity (from the perspective of C + +, it is a pointer from the perspective of assembly);

It must be initialized, and the target cannot be changed; Constant attribute requirements are consistent and can be more stringent, int a; const int &ra= a;

Constant references (Universal references) extend the lifetime of temporary variables.

Reference parameters and constant reference parameters:

Avoid the overhead of copying objects;

Often reference formal parameters: prevent modifying arguments; Accept constant type arguments, foo (a, 10);

Reference return value:Cannot return a reference to a local variable.

Type conversion: