You need to pay attention to for C ++

zhaozj2021-02-16  46

The following points are to apply for all C programmers. The reason why they say they are because these points mentioned are that you usually can't find in the C book or on the website. Such as: pointing to members, this is where many materials are not willing to mention, and it is also a place where it is wrong, or even some advanced C programmers. The main point here is not only how to explain how to write better code, but more is something inside the language rules. Obviously, they are permanent in C programmers. I believe this article will make you harvest. First, I will return some questions from different levels of C programmers. I am surprised to find a lot of experienced programmers, I haven't realized that Hymbols should also appear in the standard header file. Key 1: or ? Many C programmers are still using rather than using the updated standard library. What is the difference between these? First, we started against the. H symbols in the standard header file. Continue to use outdated rules are not a good way. From a functional perspective, includes a series of templated I / O classes, which is just that the character stream is only supported by . In addition, the C standard specification interface of the input and output stream has been improved in some subtle details, so and are different in interfaces and executions. Finally, the various components of are declared in the form of STL, however, all components of are declared into global types. Because these substantially different, you cannot confuse these two libraries in a program. As a habit, in the new code, it is generally , but if you deal with the code written in the past, in order to inherit the consistency of the old holding code to continue with . Point 2: When you pass parameters, you should pay attention to the parameter with reference to the reference, it is best to declare the reference to the const type. The advantage of this is: telling the procedure that this parameter cannot be modified. In this example, the function f () is the passed reference: Void F (Const Int & I); int Main () {f (2); / * ok * /} This program passes a parameter 2 to f () . At runtime, C creates a temporary variable of the Int type value of 2, and delivers its reference to f (). This temporary variable and its reference from F () called start creation and exist until the function returns. When you return it, you will be removed immediately. Note that if we don't add a Const qualified word before reference, the function f () may change the value of its parameters, which is more likely to make the program unexpected behavior. So don't forget const. This point is also applicable to user-defined objects. You can add a reference to the temporary object if it is const type: struct a {}; void f (const same & a); int main () {f (a ()); // OK, passed a temporary A CONST reference} Point 3: "Command separation" expression form "comma separation" expression is inherited from C, used in the for- and while-cycles. Of course, this grammar rule is considered not intuitive. First, let's take a look at what is "comma separation" expression form. An expression consists of one or more other expressions, separated by a comma, such as: if ( x, --y, cin.good ()) // Three expressions this IF condition contains three comma Separated expression.

C will calculate each expression, but the result of the complete "comma separation" expression is the value of the rightmost expression. Therefore, only when cin.good () returns True, the value of the IF condition is TRUE. Here is another example: int J = 10; INT i = 0; while ( i, --j) {// until j = 0, the loop ends, when cycles, I continuously} points 4, Use the constructor of the global object to call the function before the program startup There are some applications that need to be called other functions before starting. For example: the transmissive process function, the registration function function must be called before the actual program is running. The easiest way is to call these functions through a constructor of a global object. Because global objects are constructed before the main program begins, these functions will return results before main (). Such as: class logger {public: logger () {activate_log (); // Translator Note: Call the function}}} you need to run in the constructor; Logger log; // A global instance int main () {record * PREC = read_log (); // Translator Note: Read the log file data // .. Program code} The global object log is constructed before the main () runs, and the log calls the function activate_log (). Therefore, when main () is executed, it can read data from the log file. There is no doubt that memory management in C programming is the most complicated and easier to show bugs. Direct access to raw memory, dynamic allocation storage, and maximum playing C instruction efficiency, making you try to avoid BUG related to memory. Totel 5: Avoiding a pointer to a pointer to function using a complex constructor is one of the syntax that is the worstable syntax in C . Can you tell me what the following statement is? Void (* p [10]); P is a "10 pointers constituted by 10 pointers to a returning Void type and points to another array of functions without returning and arithmetic functions." This troublesome syntax is really difficult to identify, isn't it? You can actually declare the functionality equivalent to the above statement via TypedEf. First, use the typedef declaration "Pointer": typedef void (* pfv) (): typedef void (* pfv) (); then, declared "another function pointer to no return and using PFV": typedef void (* pf_taking_pfv (PFV); Now, a array of pointers composed of 10 tops: PF_TAKING_PFV P [10]; with Void (* p [10]) (void (*)) reaches the same effect. But this is not more readable! Point 6: Pointing to a member of a class with two basic members: Function members and data members. Similarly, there are two pointers that point to members: pointing to the pointer of the function member and pointer to the data member. It is not commonly used because the class generally does not contain public data members, which is only used when the coordination structure and class (Class) are used when inheriting CLASS. The pointer to the member is one of the most difficult constructs of the C syntax, but this is also a C most powerful feature. It allows you to call a class of function members without having to know the name of this function. This very agile calling tool. Similarly, you can also check and change this data without knowing its members' name by using a pointer to the data member.

Pointing to data members Although the grammar of the pointer pointing to the member will make you a little confused, but you will soon find that it is actually similar to ordinary pointers, but it is the * number :: symbol And the name of the class, an example: Define a pointer pointing to the int * Pi; defines a data member pointing to the INT type class: int A :: * PMI; // PMI is an INT type of class A Members you can initialize it: class a {public: int Num; int x;}; int A :: * PMI = & a :: Num; the above code is a NUM member that pointing a INT type pointing to class A and Initialize it to this NUM member's address. By adding * you can use and change the value of the NUM member of Category A: A A1, A2; INT N = A1. * PMI; // Put a1.Num Assign the value to Na1. * PMI = 5; // Assign 5 assignments to the A1.Num A2. * PMI = 6; // Put 6 assignment to 6A2.NUM If you define a pointer to class A, then above You must use the -> * operator instead: a * pa = new a; int N = pa-> * PMI; PA -> * PMI = 5; pointing to the pointer to the function member composed of data type returned by the function member, class Famous follow :: symbol, pointer, and function's parameter list. For an example: a pointer to a function member of class A (this function returns an int type): Class a {public: int func ();}; int (a :: * pmf) (); the above definition is to say PMF is a pointer to the function member of the class A. In fact, this pointer and a pointer to a normal pointing function have no difference, just it contains the name of the class and :: symbol. You can call this function in any * PMF (): PMF = & a :: func; a a; (a. * Pmf) (); // call a.func () If you define one first Pointer to the object, then the above operation is used to use -> * instead: a * pa = & a; (pa-> * pmf) (); // Call PA-> FUNC () pointing to the pointer to the function member to consider polymorphism Sex. So, this call will be dynamically recycled when you call a virtual function member through a pointer. Another place to pay attention to, you can't take a class constructor and the address of the destructor. To point 7, avoiding the occurrence of internal preparations often: Your application will generate memory vulnerabilities due to the program own defects, and you repeat your programs during the period, I want to know that it will also crash the system. But how can I prevent it? First, try less dynamic memory. In most cases, you may use static or automatic storage or STL containers. Second, try to allocate a large block of memory rather than only a small amount of memory. For example: allocate the memory required by an array instance, not a memory of an array element at a time. Totels 8. It is delete or delete [] There is an absurd statement in the programmer: Use Delete instead of delete [] to delete an array type! For example: int * p = new int [10]; delete p; // error, it should be: delete [] p The above program is completely wrong. In fact, using delete instead of delete [] on a platform may not cause the system to crash, but that is purely luck. You can't guarantee that your app is compiled on another compiler, run on another platform, so please use delete [].

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

New Post(0)