Background
C + + is the main language used by Google's open source projects, although Google Code is yellow. C + + is a powerful language that adds to its complexity. In order to let the code by other programmers understand, reduce the possibility of a bug, so with this Google C + + style guidance.
Header Files
- Each. cc file should be associated with an. h file, unless it is a unit test or contains only one main ();
The #define Guard
- Each header file should be avoided for repeated inclusion. The macro definition can be in project_path_file_h_ format.
For example:
#ifndef Foo_bar_baz_h_
#define Foo_bar_baz_h_
...
#endif//Foo_bar_baz_h_
- Do not use # include in cases where the predecessor declaration is sufficient. A change to the header file will cause the file containing it to be recompiled, so it is recommended that the minimum inclusion principle, especially the header file containing the other header files, be included.
For example, when you need to use the file class but do not need to use the definition declaration of the class, you can use the predecessor declaration class file in the header file instead of the #include "file/base/file.h"
How to use the Foo class when the header file does not get the Foo definition?
- Declare data members using Foo * or Foo &.
- You can define and return values in a function declaration.
- With static data members, static members are defined outside the class.
But if your class is a subclass of Foo or has a data member of type Foo, it contains the header file. Do not overdo the loss of header file inclusions, resulting in increased code complexity and performance consumption.
Google C + + Style Guide Read NOTE 1