C + + Programming Specification-header file

Source: Internet
Author: User
Tags class definition parent directory readable

When choosing a programming specification, I preferred Google, followed by Huawei and Microsoft, and finally adjusted according to some of its own realities. The following is an excerpt from Google's C + + programming specifications.
——————————————————————————
Typically, each. cc file (the source file for C + +) has a corresponding. h file (header file), with some exceptions, such as unit test code and. cc files that contain only main ().
Proper use of header files can make your code significantly more readable, file size, and performance. The following rules will guide you through the hassle of using header files.

1. Protection of #define
All header files should use a # define to prevent the header file from being multi-contained (multiple inclusion), and the naming format should be:

<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 Foo/src/bar/baz.h in Project Foo is protected as follows:

#ifndef FOO_BAR_BAZ_H_#define FOO_BAR_BAZ_H_...#endif // FOO_BAR_BAZ_H_

2. The header file Dependency
uses 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 would prefer to include as few header files as possible, especially those contained in other header files. The
use of the predecessor declaration can significantly reduce the number of header files that need to be included. Example: The header file uses the class file, but does not need to access the file's declaration, the header file only need to declare a class file, no # include
"File/base/file.h".
How do I use Class Foo in a header file without having to access the definition of the class?
1) Declare the data member type as Foo * or foo &;
2) arguments, functions that return a value of type Foo are only declarations (but do not define implementations);
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 makes more sense to substitute object members (object member) with pointer members (pointer member, if SCOPED_PTR is better). However, this approach can reduce code readability and execution efficiency. If you just want to include the header files, do not replace the 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: You can rely on the declaration to not rely on the definition.

3. Inline function
defines an inline function only if the function is only 10 rows or less.
Definition: When a function is declared as an inline function, the compiler may expand it without invoking the inline function as usual function call mechanism.
Advantage: When the function body is relatively small, inline the function can make the target code more efficient. For access functions (accessor, mutator) and some other short key execution functions.
Cons: Misuse of inline will cause the program to become slower, and inline may be the amount of target code or increase or decrease, depending on the size of the inline function. Inline shorter access functions typically reduce the amount of code, but inline a large function (the translator notes: If the compiler allows it) will dramatically increase the amount of code. On modern processors, small code tends to perform faster due to better use of the instruction cache (instruction cache).
Conclusion: a more appropriate processing rule is to not inline more than 10 rows of functions. For destructors to be treated with caution, destructors tend to look longer than their surface because there are some implicit members and base class destructors, if any, that are called!
Another useful processing rule: inline those functions that contain loops or switch statements are not worth the candle, unless in most cases these loops or switch statements are never executed.
It is important that virtual functions and recursive functions are not necessarily inline functions even if they are declared inline. In general, recursive functions should not be declared inline (translator Note: The expansion of the recursive call stack is not as simple as looping, such as recursive layers may be unknown at compile time, and most compilers do not support inline recursive functions). The main reason for the destructor inline is that it is defined in the definition of the class, to facilitate or to document its behavior.

4.-inl.h file
The definition of a complex inline function should be placed in a header file with the suffix named-inl.h.
The definition of an inline function is given in the header file, which enables the compiler to expand it inline at the call. However, the implementation code should be fully placed in the. cc file, and we do not expect too much implementation code in the. h file, unless it has a clear advantage in readability and efficiency.
If the definition of an inline function is relatively short and logically simpler, its implementation code can be placed in the. h file. For example, the implementation of access functions is, of course, placed in the class definition. For ease of implementation and invocation, more complex inline functions can also be put into. h files, and if you think this will make the header file cumbersome, you can also detach it into a separate-inl.h.
This separates the implementation from the class definition and, when needed, contains the-inl.h that the implementation resides in.
The-inl.h file can also be used for definition of function templates, which makes the template definition more readable.
One thing to be reminded of is that-inl.h, like other header files, requires a # define protection.

5. function parameter order (functions Parameter ordering)
When defining a function, the parameter order is: the input parameter is before, and the output parameter is behind.
C + + function parameters are divided into input parameters and output parameters, and sometimes input parameters will be output (translator Note: When the value is modified). Input parameter general or constant reference (const references), output parameter or input/output parameter is a very few pointer (Non-const pointers). When a parameter is sorted, all input parameters are placed before the output parameter. Do not put it in the last place simply because it is a newly added parameter, but should still precede the output parameter.
This is not a rule that must be followed, and the input/output dual-use parameters (usually class/struct variables) are mixed in, making the rules difficult to follow.

6. Name and order of the included files
Standardize inclusion order to enhance readability and avoid hidden dependencies (hidden dependencies, translator note: Hidden dependencies are mostly compiled in the included files), in the following order: C libraries, C + + libraries, other libraries. h, in-project. H.
The header files within the project should be arranged in the project source code directory tree structure and avoid using UNIX file paths. (current directory) and: (parent directory). For example, google-awesome-project/src/base/logging.h should be like this
Are included:
#include "Base/logging.h"
The main function of dir/foo.cc is to perform or test dir2/foo2.h functions, foo.cc contains header files in the order
Under
Dir2/foo2.h (priority location, details below)
C System files
C + + system files
Other Library header files
Header files in this project
This sort of sorting method effectively reduces the hidden dependencies, and we want each header file to be compiled independently. The simplest way to implement this is to include it as the first. h file in the corresponding. cc.
Dir/foo.cc and Dir2/foo2.h are usually located in the same directory (like base/basictypes_unittest.cc and base/basictypes.h), but can also be in different directories.
The same directory under the head file alphabetical order is a good choice.
For example, the google-awesome-project/src/foo/internal/fooserver.cc contains the following sequence:

#include "foo/public/fooserver.h" // 优先位置#include <sys/types.h>#include <unistd.h>#include 

————————————————————————————
I use the blog as a notebook, the content will change with the actual situation.

C + + Programming Specification-header file

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.