Some suggestions on writing C++ code in "C++ Programming Language (Fourth Edition)"

1. The use of constructor/destructor can simplify the management of resources;

2. Preferential use of standard library containers and algorithms;

3. Prioritize the use of the features of the standard library rather than the code written by yourself;

4. Use move semantics to avoid copying large objects;

5. Use unique_ptr to refer to polymorphic objects;

6. Use shared_ptr to reference shared objects;

7. Use templates to maintain static type safety (eliminate type conversion);

8. Don't use macro definitions, use const instead;

9. Re-define variables (local) when needed, and initialize them immediately after definition;

10. Do not use C language functions such as malloc() and free(), new and delete are better than them;

11. Do not use arrays and C language style strings as much as possible (char * p = "xxxxxxxxx";), it is better to use the standard library array, vector, string;

12. Except in special code (such as memory manager) or simple array traversal (++p), avoid performing arithmetic operations on pointers.

13. Try not to use unions.

14. Create objects in stack memory as much as possible.

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113772097