1. The alignment principle. For example, 64-bit bus, each address read 8B. Pay attention to the variable address when programming, and consume the least number of addresses of the bus. Heap memory request, the system is strictly aligned according to the principle of alignment, so use time also try not to cross the addressing boundary.
2. When needed, the code can be copied for efficiency, although the code volume is increased, but it is worthwhile. Especially for loops, if the number of times is less, it is no harm to disassemble.
3. Bit operation,-1 right shift, left 1, it is still -1;-1 left shift, right 0, it is no longer 1.
4. Each application of the heap memory, preferably initialized, the inside is garbage data, and not empty.
5. In project development, often an engine exposes a pure virtual class, and its interface is the * * pointer variable of this class.
6. Program logic, heavy in semantics. You cannot reduce the design of a function for the simplistic code.
7. *& represents a reference to a pointer.
8. A static method of a class cannot invoke its non-static method, nor can it invoke a non-static member variable.
9. When multiple files are programmed, the header files cannot be included with each other.
10. Try not to use the using namespace STD in the header file;
The static member definition is placed inside the CPP file instead of in the header file.
12. Pure virtual class try not to extend more than two layers.
Include references as much as possible in CPP files.
14. Subclasses inherit the parent class and cannot initialize the members of the parent class that are not initialized in the constructor in the constructor, that is, the members that can be initialized in the subclass constructor, only their own and parent constructors.
15. In project development, for some parameters that depend on the local environment, write a special configuration file, such as a server address.
16. The header file is only declared, not defined. In the header file, the global variable declaration must be modified by the extern modifier, the static member variable declaration placed in the header file, the definition placed in the CPP file, if the normal static variable, preferably declared and defined in the CPP, because the static scope is limited to a single file, placed in the CPP is only visible to the file, The header file will be copied by all CPP that references the header file in exactly the same variable.
Under Linux, when file operation, file path to take absolute path (relative path is often a bug), the file pointer to its return value to judge, to prevent null pointers.
It is good to use assert in debug state, but remember to add a # define NDEBUG,ASSERT statement to # include <cassert> before the release version will be affected by Ndebug. Here is a word of mouth, errors and exceptions are different, exceptions are unavoidable, in the release version of the indispensable, so assert cannot be used to handle exceptions. Note: After adding the # define Ndebug, both debug and run, the Assert statement will be ignored directly, so it is usually developed to comment out the # define NDEBUG, and then enable it when released.
19. Object-Oriented Programming: maintainable, reusable, scalable, and flexible.
20. In frequently used code, use as few containers as possible because of the overhead of creating and reclaiming them.
string splicing efficiency: Tested (http://bbs.csdn.net/topics/360000434 and experiments in my own project), memcpy is the most efficient, the string class + = efficiency is particularly high enough to meet everyday needs, and strcat efficiency is very poor, append also unpleasant.
the virtual functions in the base class are either implemented or pure virtual functions (the declaration is not allowed to be implemented or pure virtual).
In the C + + class, the ordinary member function cannot be used as a pthread_create thread function, and if it is to be a thread function in Pthread_create, it must be static.
24. When multiple threads access the same class static method, it is no harm if the method only operates on stack data, because the thread stack is independent, and if the method operates on non-stack data (such as heaps, global variables, etc.), a mutex is required.
25. Inline function, define the body to be placed in the header file, to ensure that each of the CPP referencing the header file is identical copy, the inline keyword is placed before the definition, do not have to be placed before the declaration, if defined within the class, you do not need the inline keyword.
When vector executes clear, the destructor for the element (if it is a class) is automatically invoked.
27. When programming, the variable (especially the global nature of the variable or class) is named, to be interpreted, but not with arbitrary J1, i1,n,m and other names, easy to conflict with the variables in the library.
28. When defining macros, try to use as many integers as possible, less with -1,-2 and so on, susceptible to the confusion caused by the non-unification of uint and int.
29. When a function parameter takes a default value, it is written in the declaration, not in the definition, so that it is convenient to call the CPP of its header file to see the default value.
Utf-8 Chinese encoding Three bytes denotes a Chinese character
32. When the project is developed, use conditional compilation:
#define DebugMain () {#ifdef debug printf ("debugging\n"); #else printf ("notdebugging\n"); #endif printf ("running\n");}
When you publish a version, comment out the first line. This is easier than opening a lot of annotations.
C + + Programming optimization experience (continuous update)