Understanding and practice of the Google C + + programming specification

Source: Internet
Author: User

1. #define保护
All header files should use a # define to prevent header files from being multi-contained (multiple inclusion), named in the format:

<project>_<path>_<file>_h_

To ensure uniqueness, the name of the header file should be based on the full path of the source code tree in which the project is located. For example, the header file in Project Foo

The foo/src/bar/baz.h is protected as follows:
#ifndef Foo_bar_baz_h_
#define Foo_bar_baz_h_
...

#endif//Foo_bar_baz_h_

This rule can help us solve a lot of trouble, insist that the application of this rule should be a good programming style, especially in large-scale projects, the header file too many, the repetition may be very high.

2. Header file dependencies
Use the predecessor Declaration (forward declarations) to minimize the number of # include in. h files. When a header file is included, a new dependency (dependency) is introduced, and the code is recompiled as long as the header file is modified. If your header file contains other header files, any changes to these header files will also cause the code that contains your header files to be recompiled. Therefore, we should include as few as possible the header files, especially those contained in other header files.
Using a predecessor declaration can significantly reduce the number of header files that you need to include. Example: The header file uses the class file, but does not need to access the file's declaration, the header file only need to pre-declare the class file, do not need to include "file/base/file.h".
How does the header file do with class Foo without having to access the definition of the class?
1) Declare the data member type as Foo * or foo &;
2) The function of the parameter and the return value type Foo is only declared (but does not define the implementation);
3) The type of a static data member can be declared as Foo, because the definition of a static data member is outside the class definition.
On the other hand, if your class is a subclass of Foo, or contains a non-static data member of type Foo, you must include the header file for it. Sometimes it is more meaningful to substitute object members (object member) with pointer members (pointer, if SCOPED_PTR is better). However, this approach can reduce code readability and execution efficiency. If you want to include only the header files, do not
This alternative is good. Of course, the. cc file requires a defined portion of the class that is used anyway, and it will naturally contain several header files.

Note: It is not dependent on the definition to be able to rely on the declaration.

This rule works far beyond what we thought it would do (at least in my own experience). In a medium-sized project, the header file is included to include, and it is likely that a ring will be formed. For example, A.h contains a b.h,b.h that contains C.h, and C.h also contains A.h. This forms a ring. This loop contains compilation errors that are often confusing, such as the one I encountered (Environment for Windows xp+vs2005): Fatal error C1189: #error: fd_setsize must be defined to 1024. The error is that fd_setsize must be defined as 1024. The general header file used in the situation as shown above, directly with the predecessor declaration is good, do not need to include the head file included, it is easy to cause the loop to include, but also introduce new dependencies.

In summary, Google's C + + programming specifications are indeed very good recommendations, worthy of careful study and practice.



Understanding and practice of the Google C + + programming specification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.