Recently, in order to find a job to participate in a number of company's written and interview, found that the previous knowledge, although learned a lot, but not in depth and system. So I'm going to read some books again, and I'm going to make some summaries, after all, the ancestors taught us to "learn and do not speak."
1. Divide the program into modules
When we do the program is generally divided into a number of modules to do, because this can guarantee the independence between the modules, not because of a module changes affect the entire program. So the most important thing we do when we divide the module is in a series of related processes (functions) and the data sets that they use you weave together, in C + +, typically in a namespace or in a class. Each module should provide a concrete implementation of the interface's hidden data and function functions, allowing the user to see only the function of the module. Simply speaking, like driving a car, we just have to manipulate the steering wheel, throttle, and so on, and the car inside the specific how to ignition, start the engine, tire rotation of the specific process hidden up.
2. virtual function mechanism
When the subclass and the parent class have similar functionality and are not exactly the same, we want to be able to invoke the subclass's function with the parent class, giving the program greater flexibility. C + + provides a virtual function mechanism for us to solve this problem. When using virtual functions in a class, each class produces a table with pointers to these virtual functions, which is the virtual function table (VTBL). The runtime can invoke a virtual function whenever it is called to find the relevant function location in VTBL based on the virtual function name. However, when a class uses virtual functions, each object of the class has a pointer to VTBL.
3. A template is a compile-time mechanism, which means that it replaces "T" in <typename t> with a specific type (int,string, etc.) at compile time. As with code that is written manually, it does not cause runtime overhead.
4. Enumeration
We often use enumerations to represent some common constants. Enumerations still have their own independent scopes, and if the values in the enumeration are non-negative, the value of the enumeration is [0:2k-1],2k is a power that can include the smallest 2 of all enumerated values. If there is a negative number, it is [ -2k:2k-1]. For example: enum E1{a = 3, b = 9}, and its scope is 0~15.
5. Initialization
At initialization time, if no initializer is provided, then static objects (including global, namespace-local, locally-static objects) are automatically initialized to the appropriate type of 0, while local objects and dynamic objects established in free storage (through new, malloc-created objects) are not initialized with default values.
6. String constants
In the program, the compiler will treat the string constants as "an array of the appropriate number of const character types", as "Xsk" is the const CHAR[4]. It is syntactically permissible to assign a string constant to a string pointer, but trying to modify a string constant by a pointer is an error, and if we want to modify the string constant, we need to copy the string into the char array.
Char *p = "Xsk"; char a[] = "Xsk";p [0] = ' s '; Error, assigning a value to a constant does not make sense a[0] = "s"; Correct, A is an array of 4 characters
In addition, string constants are statically assigned, so you can act as a function return value. An empty string is written as "", and its type is const CHAR[1]; But when you put a null character (' s ') into a string, the program only ends with a null character. For example, "xsk\0coding" will only be treated as "Xsk".
7. Constants
(1) The keyword const can be added to the declaration of an object, making the object a constant. Because const constants cannot be assigned a value, they must be initialized. (const int i = 100;)
(2) const constants are usually values that are constant expressions, and if so, constants can be evaluated at compile time, even without allocating memory for const constants.
const int c1 = 1;const int c2 = f (3); Compile time does not know the value of C2, need to allocate space extern const int c3; Compile time does not know the value of C3, need to allocate space
In addition, for const constant arrays it is necessary to allocate storage space, because the compiler cannot figure out that the expression uses those elements in a constant array, usually putting a constant array into read-only memory to improve efficiency.
(3) We can assign the address of a variable to a constant pointer, but we cannot assign the constant address to a normal pointer, which allows the constant to be modified, but it is wrong.
8. Pointers and Arrays
(1) The implementation of pointers is a mechanism that wants to map directly to the machine on which the program is running.
(2) in C + +, the general null pointer is denoted by "0" without "NULL".
(3) = = when applied to a pointer, = = compares the address, not the object to which it is pointing.
(4) between an array and a pointer, there is only a pointer to the array name that is converted to the starting element of the group, and the pointer cannot be assigned to the array name.
(5) For void* pointers, operations other than assignment and void* pointers to other types of pointers are unsafe because the compiler does not know the specific type of the void* pointer and must be explicitly converted when used. Neither the function pointer nor the member pointer can be assigned a value to the void* pointer.
9. References
(1) To ensure that we can use the reference successfully, we must initialize the reference and cannot make changes. A reference similar to a pointer can be the same as "&RR" to obtain a reference to the object address, but the reference is not a real object cannot manipulate the reference like an action pointer.
(2) The initial value of the normal reference "t&" must be a defined variable, and the initializer for the const t& may not be a variable or even a type.
double &r = 0; // error, must be a variable Const double &r2 = 2; // correct. / * 1. Convert 2 to the implicit conversion of Double 2. The result is stored in a double temporary variable of 3. Use temporary variables as initial values */
(3) A constant reference is preferred as a parameter in a general function. It is often unreasonable to distinguish a reference to a variable and a reference to a constant, to refer to a temporary variable when a variable is referenced, to make a reference to a variable that is about to disappear, to be error-prone, and to have a constant reference easily modified by a function as a function parameter. These problems do not occur for references to constants.
10. Operators
(1) for operator precedence, [scope resolution symbol] > [member Selection symbol, type conversion character and value++ symbol] > [sizeof, New, & Take address, ++value, etc.] > [member selection symbol] > [two-tuple operator] > [Displacement symbol] > [greater than, less than symbol] > [equals sign] > [bitwise logical Symbol] > [logical Symbol] > [assignment symbol].
(2) The result of an operand with an lvalue is still a left value. However, there are times when you should pay attention to the following situations:
int *q = & (x + +); // Error: X + + is not an lvalue (the value is not stored in x)
11.new and delete operators
(1) After new, delete must know the amount of space allocated to the object, which means that the new object allocates more space than a static object, usually requiring a machine word to save the object's size.
(2) operator new generally does not initialize the returned storage and throws the Bad_alloc exception by default when new cannot find the spatial allocation.
12. Functions
(1) The inline descriptor tries to give the compiler a hint that all of the inline Func functions are processed in real time, resulting in a result. Recursive functions cannot be processed in real time.
(2) Character constants, constants, and parameters that need to be converted can be passed to Const&, and cannot be passed to non-const&. If an array is passed as a function parameter, the first element pointer of the array, type t[] is converted to t* when passed as a parameter.
(3) Whenever a function is called, a new copy of all parameters and local variables is created, and after the function returns, the memory is used for other purposes. Therefore, you must never return a pointer or reference to a temporary variable.
(4) When a function pointer invokes a function and initializes it, it must require that the parameter type and the return value type be exactly the same.
13. Namespaces and
Double Prim (bool); Double term (bool); using Lexer::get_token; using Lexer::curr_ok;}
(2) Resolving potential conflicts in a biased manner
namespacehis_lib{classString {/*...*/}; classVector {/*...*/};}namespaceher_lib{classVector {/*...*/}; classString {/*...*/};}namespacemy_lib{using namespaceHis_lib;//everything from His_lib. using namespaceHer_lib;//everything from Her_lib. usinghis_lib::string;//resolve conflicts in a way that favors his_lib usingHer_lib::vector;//resolve conflicts in a way that favors her_lib classlist{/*...*/};}
(3) We generally separate the code that handles the error from the "normal" code. It is dangerous to handle errors at the same level of abstraction that leads to the wrong code. The code that completes the error handling is likely to cause error handling errors.
The road to coding--re-learning C + + (1): summary of C + + basic knowledge blind spots