100 C ++ knowledge points per week (memo) (iii)

001-019 function template

1. The function template is a generic description of a function is defined using a generic function. As defined manner: ① (C ++ after 98) template; ② (C ++ 98 previously) Template

2. In the parameter types but different functions required for operation of the same function, use the function template will be good. Generating a plurality of function definitions make it easier and more reliable. At the same time, most of the templates in the header file (the header file is the successor content)

3. template overloads: different types of different implementations can be defined template overloaded. Template definition parameter must not be generic, it can be a specific type

4. Use the template limitations: Some types may not be processed, it is necessary to use: ① overloaded operator (Chapter 11); ② provide specific templates that are defined as a specific type, that is, concrete

The explicit embodied: Purpose: to assign a structure to another structure, the generic template may be used, but it is assumed only some content structure is operated, this time using the heavy load is not feasible, a solution explicit concrete, i.e. a concrete function definition

Rule 6: ① For a given function name, there may be a non-template function, the template function template specific and explicit functions and overloaded version thereof; ② explicitly embodied prototype and should start with the definition template <>, and to indicate the type defined method by name: 1> template <> return_Type func_Name (type &, type &), or 2> template <> return_Type func_Name (type &, type &); ③ embodied in preference to conventional template, a non- template function takes precedence over the concrete and the Normal template

7. Initially, the compiler implicitly instantiated using the template to generate function definitions (and normal function), now allows explicit instantiation, i.e. specify the desired type of compiler. ☆ ★ ☆ differences distinguish explicitly instantiate explicit embodied ★ ☆ ★, the syntax is: explicitly instantiate: template returen_Type Name (type, type). Explicit embodied: template <> return_Type Name (type, type), or return_Type Name (type, type)

8. explicit instantiation means: the use of a definition of a function template generating function type; explicit concrete means: not applicable function templates to generate function definitions and function used for a special type defined explicitly defined .

9. Use of the same type in the same file or explicit conversion unit and explicit examples of concrete error will

10. Examples of implicit, explicit and explicit examples of particularly referred to as concrete, which are defined using a specific type of function, rather than a general description. Note that the use of template and template <> differentiate and concrete examples. May also be used to create function in the program explicitly concrete syntax is Name (parameters)

11. Which function version of the compiler choose? The selection process is called overloading resolution. : ① From create a candidate list of functions included in the same called function name of the function and template functions; ② create viable function list using the candidate list of functions, i.e., the number of parameters of correct function, there is an implicit conversion sequences for suitable type conversion; ③ determine whether there is the best possible function, if the call or the call wrong

12. When the matching sequence compiler selection function version (optimal to worst): ① exact match, but in preference to the conventional function template; ② enhance conversion, such as conversion to char Int, float convert double; ③ standards conversion, such as int converted to char, long converted to double; ④ user-defined conversion, as defined in the class declaration conversion

13. If there is a plurality of exact match error (error content may include ambiguous ambiguity), but there is still a special case, assume that a parameter of char, and the other is const char &, then the two are exactly matched. Thus there are some "insignificant conversion", including const, volatile, * <-> [], & convert

14. For the above-described ambiguity, if and only if the argument is a reference and a pointer to a non-const const priority, precedence over non-template function template function, when such an error does not occur ambiguity; exact match if the two are a template function, the template function more specific priority, i.e. explicit priority over the use of specific template embodied implicitly generated

15. To identify the "most specific" template, which means less need for parameter conversion function "more specific." Find out the most specific part of the collation rule template is called a function template

16. A custom selection function is executed, may be used when using the function: name <> (params) this form, the compiler will call the function to select a template; name (params) may be, which is an explicit function instantiated

17. For the process can not determine the type of variables, C ++ provides a method: decltype (expression) var ;; ① is assigned var type expression, if the expression is a function ②, var is the type of function return values, ③ If the expression is left value and starting with the brackets, the point to its type var-rEFERENCE

18. If you need to repeatedly stated, you can typedef decltype (expression) varname;

19. For the return type of the function does not know, the syntax can be used: auto name (params) -> type ;, in conjunction with decltype identifier, can be written as: template <class T1, class T2> auto gt (T1 x, T2 y ) -> decltype (x + y) {... return x + y;}

020-028 compiled separately

20.UNIX and Linux systems provide the make program to program and tracking files since the last modification time of these documents; VS provides a similar tool in Project: Solution Explorer

21.C ++ program can be divided into the following structure: ① header file that contains the structure of the declaration and use of these structures function prototypes; ② source code file: contains the code related to the structure and function; ③ source file: Contains call and structurally related the code for the function

22. Do not define the function or variable declarations in header files ★★★★★ because if contains a function defined in the header file, the same program will include two definitions of the same function unless the function is inline otherwise error. Usually header file comprising: a function prototypes, or using #define defined symbolic constant const, declaration structure, class declaration, a statement template, inline functions. Principle: ① structure declaration does not create a variable, just tell the compiler how to create variables; not the templates will be compiled code, but indicates how the compiler to generate function definitions and function calls in the source code matches; const data and inline functions have a special link properties

23. Use <> is enclosed in the standard header files, the host system will look for files are stored in the standard header files; use '' is enclosed in a user-defined header files, the compiler first current working directory to find the source code, or directory (depending on the compiler), if not found, then the standard position find

24. ★★★★★★ not be added to the list of items header files (#include the header file management), nor again the source code files using #include to include other source files (resulting in multiple declarations)

25. The same header file will contain only once in a file (program), but will generally include the use #ifndef xxx and #endif #define xxx and this group of statements can check the header file. This approach will not prevent the compiler header files included twice, but ignore all the content for the first time included in addition to outside

26.stdafx.h play a role in C ++ precompiled header files, namely the C ++ header files used in the project pre-compiled, after compiling the project, the direct use precompiled result, the rate of increase. After any code should be placed #include "stdafx.h"

27. Glossary files are separately compiled translation units

28. The link multiple libraries, due to different compilers may produce different file name modification, resulting in the function definition matching the linker can not generate a compiler function calls with another compiler-generated, so to compile a run once under control

029-044 persistent storage, the scope and nature link

29.C ++ continuous variables (data retention time in memory): variable (parameter) ① continuous automatic storage function definition declared, finishing the execution of the function or pieces when the memory is released; ② continuous static storage function definition and variables defined outside static keyword defined variables, running throughout the program exists; ③ continuous thread storage, use keyword thread_local declared variable, as long lifecycle thread; ④ sustained dynamic storage resistance, the use of the new operator will allocate memory exists until use delete to release, sometimes referred to as free storage or stack

30. Scope: name described how visible inside the scope of the document; linkability: how to describe the name of the shared among different units, the external file name may be shared between only a shared file name inside the function . Automatic variable name is not linked sex

31. Scope: ① local scope, it is defined only available block of code (called a code block enclosed in curly braces); ② global scope, defined position to the end of the file are available; ③ automatic variable scope is local, static variable scope depends on how it is defined, the name of the role of function prototypes used only in the domain that contains a list of the available parameters in parentheses, the scope of the class members are in the class of a variable declared in a namespace the entire domain name space

32. In the default, persistent storage function parameters and variables declared in a function for automatically, as a local scope, no link of

33. For automatic variables, only define their function, the block of code can be used. If an automatic variable defined outside the function, the function has defined in a different automatic variable name, then the function can access two variables, but not accessible outside the function defined in the variable function; if the function is defined within a variable with the same name, the function of the variable is accessed, will only access within the function definitions, but outside the same variables are a function of failure (defined hidden) within the function

34.auto keywords originally used to explicitly noted that variable is automatically stored in a local variable, and in the C ++ 11 standard, type inference Auto keywords for automatically

35. The automatic initialization of variables: declaration may be used in any known expression is automatically initialized variables

36. The automatic variables stored on the stack, the new data on top of the original data (the adjacent memory cell); stack uses two track pointers, a bottom in the stack (initial position), the other in the top of the stack (available memory unit ), when the function is called, the stack is automatically added to the variable, stack pointer points to the next available memory location after the variable, the function ends, the stack pointer is reset to the value before the function is called, the variables used to release a new RAM

37. LIFO stack is a rear advanced, so that can be used to simplify the transmission parameters

38. The register variables (C ++ 11 is no longer used): Use the keyword register statement, but now this keyword to explicitly declare variables automatic variable, the variable name may be the same as the external variables (and auto previous usage exactly the same)

39. The static continuous variables: There are three links of: ① of external links (accessible in other documents); ② of internal links (can only be accessed in the current file); ③ no link sex (only in the current function or code block access). Static variables are continuing presence during the entire program is running, longer life cycle than the automatic variable

40. The compiler allocates fixed memory block to the static variable; static if not explicitly initialize the variable, the compiler they are set to 0, default, each element or structural member and the static array are set to 0

41. Create: ① external static link of variable duration, declare it an outer code block; ② internal links of continuously variable static, declare it out of the code block, and use the static qualifier; ③ no link of the continuous static variable, declare it in the block, and use the static qualifier

42. no link performance of static variables and automatic continuous variables, except that the former is when the function is not performed, but also remain in memory

Two uses 43.static modifier: ① for topical statement that variable is no link of static variables, static represents the continued storage of (variable hold onto the stack, but at a fixed memory location); ② when a statement outside the block of code, static representation of internal links, when a static variable is already going

44. Static variables are initialized: zero initialized and constants initialized (default is 0, a single value may be a constant expression and the sizeof () operator), collectively referred to as static initialization, the processing means when the file compiler initializes variables ; dynamic initialization means that the variable is initialized after compilation

045-050 static persistent, external links of

45. The link of external variables usually referred to as external variables, their storage persistent static, scope for the entire file; external variables outside the function defined for all functions are external. Also known as global variables

46. ​​A single definition rule (ODR): variables can only be defined once. For this purpose, C ++ provides two variable declarations, one is defining declaration (definitions), and the other is a reference to a statement (statement), which does not allocate storage space to a variable, using the keyword extern declared without initializing otherwise, the statement is defined, leading to allocate storage space (may cause errors)

47. To use an external variable in multiple files, simply contains the definition of the variables in a file, but in all other files using the variables, you must declare it using the keyword extern

48. appear in the program variables and external variables of the same name, will be considered an automatic variable declared within a function only makes sense that variable (local variable hides a global variable)

49. Global variables are hidden function, access to global variables, operators using ::

50. For global variables, the general program is running completely intact, the statement frequently accessed global variables, it is best to use const qualifier. But the use of global variables make the program become unreliable, and should generally use local variables

051-053 static continuity, internal links of

51. In the static variable scope qualifier for the entire file, the link of this variable will be internal. The advantage of this is that you can use static variables and external static qualifier declares a variable of the same name, the static variable to act only on the file

52. In the multi-file program, you can (and can only be in one file) define an external variable in a file. Other files using the variable declaration using extern it

53. The external variables can be used to share data in a multi-file program; static link of variable internal shared data in the same file among the plurality of functions (the name space provides another method for sharing data); and scoped variables entire file becomes static, do not worry about the name of the scope to other files in the conflict for the entire file variable

054-054 static continuity, no link sex

54. The significance of static local variable: between the two calls, static local variables will remain unchanged (applies to banking passwords like "renewable" term). And for the first time when initializing static local variable is initialized, after which no variable initialization

055-063 specifier and qualifier

55. For information storage, hereinafter also referred to by the descriptor or keyword stored cv- qualifier provided: const, volatile, auto, register, static, extern, thread_local, mutable

56. In addition thread_local (may be combined with static or extern), use may not overlap other specifiers

57.volatile keyword tells the program not to make the following optimization: During operation, a few sentences of the program repeatedly used the value of a variable, the compiler may not find this value twice, but this value into the register cache

58.mutable keyword tells the program, even if the structure or class variable is const, one of its members can also be modified

59. By default link of the global variable is external, but the link of the global const variable is internal, it seems that C ++, just use a global static const defined as specifiers

60. If the consultation declared in the header file, and the initialization, the contents of the pre-header file of the processor included in each source file, the use of each source file has declared const Description Defines

61. Use const imply internal links, internal links means that each file has its own set of constants, each of which is defined in their respective files are private, so long as the same head includes two source code files file, they will get the same set of constants

62. If you want to define the link of a constant external, you can override the default use of internal links, grammar extern keyword is extern const type name = xxx; (by the ODR, only one place is defined, declared elsewhere )

63. The constant internal code block is available only within a block of code, do not worry about the same global constants and variable names

064-068 of functions and links

64.C ++ does not allow the definition of a function within another function, that function is stored sustained period are automatically static, that is, throughout the program have been present. By default, the function of the external links, the available shared between files. Extern available usage in the prototype described function is defined in another file, but this is optional; link using the static function of the set to internal, so that it can only be used in a file, and in the prototype and the definition of You are required to enter the static keyword

65. The static modifier, is the definition of static functions, static function will cover the externally defined, even outside the defined function of the same name, the file will also use static function

66. The non-inline function apply the ODR, for each non-inline functions, only one defined. Links of external functions, in multiple files in the program, only one file (possibly library file) contains the definition of the function, but each file using this function should include its function prototype

67. inline function from the ODR constraint, so inline function is available in header files, C ++ inline require all the same function definition must be the same

68. The compiler will first check the static function, otherwise the compiler will look at all of the program file, if found two definitions, the error, if not found, the search in the library, if you define the meaning of a library functions of the same name, the compiler will use a custom version instead of the library function

069-069 Language of links

69. If you want to use the functions in the C language library in precompiled C ++, you need to: extern "C" returnType name (params); reality can provide links of other languages, use the "" mark. Internal mechanism is the compiler of the "Name modification or correction of the name"

Dynamically allocated storage solutions and 070-080

70. Typically, the compiler uses three separate memory, a static variable, a variable for an automatic, dynamic storage for one another. C ++ uses new and delete to control the dynamic memory, C using malloc () controls the automatic storage

71. The dynamic memory storage scheme: Available tracking static and dynamic memory pointer variables automatically. In other words, the new memory allocation, will always exist, but the variable declaration will disappear at the end of a block of statements, so the address must be passed or returned to the function. But if all the function pointer declared as external links, the file is located behind the declaration can use it. In other documents, by extern modifier can also access it

72.new operator initialized (C ++ 11 standard): for use braces list initialization, the initialization list may be for a single value of the variable

73.new memory when requested can not be found, an exception is thrown std :: bad_alloc (Chapter 15 speaking)

74. The operator new and new [] are call: void * operator new (std :: size_t); and a void * operator new [] (std :: size_t); two functions, these functions are called distribution function, is located in the global namespace, delete and delete [] calls the release function. They use operator overloading, std :: size_t is a typedef, corresponding to a suitable integer

75. 74, int * pi = new int; will be converted to int * pi = new (sizeof (int)) ;, int * pa = new int [40]; to be converted to int * pa = new (40 * sizeof (int)) ;. At the same time, C ++, you can define new () function. delete related functions similar

76. The positioning of the new operator, the general responsible for finding new block of memory on the heap, use placement new operator, you can specify the location you want to use. It must include the header file, use: first address to open up a statement, and then create a structure / variable using the new operator, and finally the use of new (buffer) var / structure, to complete the positioning. As a result, the var / structure is placed in buffer

77. Positioning the memory block occupied by the new operator, if it is not a conventional new original statement, we can not use the delete delete

Another use of the new operator 78. Location: in conjunction with the initialization, such that the information on the specific hardware address

New positioning mechanism 79. The operator: returns the address passed to it, and the address is forced into void *, it can be assigned to any type of pointer. You can overload the new operator; and may be used in new operator class object

80. The function of the positioning of the new operator: function prototype is a function of two parameters comprising, new (sizeof (type), buffer) ;, wherein the first always std :: size_t, such overloads are known to define new

081-100 namespace

81. The meaning of the name space: there is better management functions, classes, variables of the same name, and information enumerations, structures, and other members of the class, the name of the scope and better control

82. declaration area: area statements may be made in which the region declared external function declarations of global variables, file declaration statement located region; variable functions declared in the code declaration block is located in the region of its declaration

83. The potential scope: the potential scope of a variable declaration from the start point to the end of its declaration area. Potential scope statement than a small area, is to use because the variable must be defined after

84. The variable is not in its potential scope are visible (hide local variables to global variables), program variables for the visible range called the scope of

85.C ++ its own rules on global variables and local variables defined in a namespace hierarchy, you can declare the name of each statement within the region, these names are independent of names declared in other regions statement

86. Create namespace namespace keyword, the syntax is namespace Name {definitions of other vars}; global name space may be, may be located in another namespace, but can not be located in the code block. Means that the namespace declaration of the name of the link is external (unless quoted constants)

87. The global name space: File-level declaration area, global variables are described as being located in the global namespace

88. To add or change an existing name or define a space variable, you can use again namespace operator and the new content using the curly braces, add the name space within the existing

89. Access values ​​of the given namespaces: using the Analytical :: operator, and to use the name to define the name space. Title name unmodified referred unqualified name, it contains the namespace qualified name called

90. The use of the name space values ​​of two ways: using declarations and using the compiler. Using declaration, a method of using Name :: var, the function can be used inside and outside the method, the former add the name to a local area life, which adds to the global namespace name; the using space such that all variables compiled are available, using namespace name ;, the compiler compiler does not allow simultaneously using two namespaces (resulting in ambiguity, Name1 :: apple and Name2 :: apple can be distinguished, but if you are using the compiler, not the two apple can be distinguished)

91. In the local name space, the same variables are declared in name space hidden variable domain may be used to resolve the operator using the variable names defined in the space, can also be used on the Analytical operator to use a global variable defined, :: difference between the two is whether the former name space

92. If the name and namespace declaration area defining variables of the same name, an error will be: If the name using compiler directives namespace declaration introduced into the region, the local version of the hidden version namespace; declared in the name space as a function of It is declared outside a function, but the scope is a function of

93. In general, using declaration than using compiler directives safer. Can use a nested namespace comprises creating a common namespace declared using; using directive may be used so that the name in the name space available inside, when nested case access variables, variables using two namespaces :: method name are available

94. If there is a nested namespace, the import of the presence of transmission, that is introduced contains a namespace other namespaces, into which is equivalent to two namespaces

95. You can use a namespace xxx = long namespace name ways to simplify (alias), this method can also be used to simplify the nested namespace: namespace xxx = A :: B :: C

96. unnamed namespace declarations from the scope of potential point to the end of the declaration area, and they are similar to global variables, but this is not the name of the name space, can not use or compiled using an explicit statement, so this namespace can only be used in this document, is the link of alternatives to the internal static variables

  1. In other words, under normal circumstances, the use of namespaces can do: First, create a header file that contains the namespace (as well as other structures, variables, etc.), and then implement functions such as content namespace in a source file, the last in the other call the source file name space

98. If a function has multiple overloaded, then the use of using declared after (also compiled), the function will import all heavy-duty version, only the name of the function declaration to explain

99. Some guiding principles: ① use variables declared in a named namespace, rather than external global variables, static global variables; ② If you develop a library or library, place it in the namespace ( such as C ++ will cmath defined mathematical library placed in the std namespace); ③ Do not use using compiler directives in header files, so let obscures what name is available, and the header file that contains the order will affect the behavior of the program, so comprising using the compiler, it is placed after all the pre-compiler #include command; ④ when introducing name, preferred to use the scope resolution operator or a method using declarations; ⑤ for using declaration, it is preferred to set the scope of a local rather than a global

100. The old header files without the use of the name space, such as iostream.h, but the new iostream header files use the namespace std

Next Issue: Getting Started with objects and classes, objects and classes really, really, wife wife wife more! ! ! QwQ, TAT. Distress, do not know to learn Qt or MFC

In addition, we should see here to try to do some projects slightly! On completion of objects and classes will be a big leap! Come duck ~

Released eight original articles · won praise 2 · Views 637

Guess you like

Origin blog.csdn.net/qq_41450779/article/details/104056540
Recommended