1. Use typedef and # define to improve readability
Who knows what complicated and difficult pointers, such as void (* p [10]) (void? * P [10] In the first bracket is a pointer array. the pointer in the array points to a function with void (*) () as the parameter, and this parameter void (*)() it is also a pointer to a function that has no parameters and does not return. For these readability issues, we can use the following method: typedef void (* pfv) () declares the function pointer without any input parameter, and typedef void (* Keypoint) (pfv) the Declaration points to the function pointer whose parameter is pfv and does not return. The Keypoint p [10] is finally completed.
Note that if there are two typedef int * PINT and # define PINT * Statements, PINT p1 and p2 may disappoint you. If typedef is used, there will be two pointers. If it is # define, there will be a pointer and an integer variable. The reason is that typedef is encapsulated.
2. prefix operators are preferred.
Here we take the auto-incrementing operator as an example. If you have the ability, you can compare it from the perspective of Assembly. Here we will look at it from the surface. Before running the operator, the compiler should look for related functions. No matter the prefix or suffix, there is only one parameter. To differentiate, C ++ specifies that the suffix has an int type parameter, and the compiler passes a 0 value to the function. Let's compare the differences between the two:
[Cpp]
// Member function overload
<Type> ClassName: operator ++ (); // prefix
<Type> ClassName: operator ++ (int); // suffix
// Overload non-member functions
<Type> operator ++ (ClassName &); // prefix
<Type> operator ++ (ClassName &, int); // suffix
// Suffix, construct a temporary object to save the original value, auto-increment, and return the temporary object
// Prefix, auto-increment, return
Although there may be little difference in efficiency, since there are good ways to save trouble, why not?
3. Differentiate the three new forms
New operator/operator new/placement new. Maybe we usually write something similar to the int * ptr = new int class, and we also wrote the classname * ptr = new classname (......) Create an object, that is, the new operator applies to the memory. But the request for memory calls operator new again, which means a bit of heavy load function, namely void * operator new (size_t size ). The last placement new is part of the C ++ standard library and is used to select an appropriate constructor. If you want to process uninitialized memory to obtain the desired object, let's do this:
[Cpp]
# Include <new>
# Include "classA. h"
Int main ()
{
Void * s = operator new (sizeof ());
A * p = (A *) s;
New (p) A (2012); // magical usage
......
Return 0;
}
</New>
It's really the first time my brother saw this kind of thing. new (p) A (2012) creates an object with A specific constructor in A specific memory and wants to use placement new for manual memory management. Summary: 1. Create an object on the stack and implement it with new operator; 2. allocate only the memory and do not do anything else. operator new. Of course, reload one if you are not satisfied; 3. Existing Memory creates an object, placement new, which is usually not the case.
4. Failed to correctly handle new
If (! Ptr ){......} . Although I have never seen it fail, but for new, it does not return a NULL pointer. Instead, it throws an std: bad_alloc exception, then we should take the new catch exception: try {int * ptr = new int ;......} Catch (std: bad_alloce) {return false ;}. Of course, the old compiler will still return NULL, but the new compiler ...... Haha, brother has never tried...
5. Time to define variables
C ++ allows variables to be defined at any position. When a definition is met, the relevant constructor is called, and the related destructor is also called when the deletion is completed. When using variables, try to avoid the expansion of the scope, which not only effectively reduces variable name pollution, but also facilitates others' reading and understanding. The obvious difference between VC6.0 and VC2010 is for (int I = 0; I <N; I ++). Here, the scope of variable I is different in two environments! In short, delay the definition of variables.
6. prevent repeated header files
When # include "classA. h" is usually required, an error occurs if the bread contains a duplicate with the existing one. The common method is # ifdef <project >_< path >_< file >_h # define ......, Of course, there is also a # pragma once method. During each compilation, the compiler must open the header file to determine whether there are repeated definitions. Therefore, in a large project, # ifdef makes the compilation time long, and # The pragma once method is provided by the compiler, it ensures that the same file will not be contained multiple times (the same file refers to the physical file, not the same content). This method does not seem to be very good in terms of compatibility, therefore, the former method is often used.
From study room