The contents of the C + + programming header file and the source file

Source: Internet
Author: User

From a smaller program to a more complex program, the header file and the content of the source file is troubled for a long time, especially in the header file in the content, search for articles and a summary, if there is any mistake or debatable place, I hope you can advise.


Introduce the problem:

Compilation mode: A program of the source code, can be placed in different files for storage, each source files are independent, can be compiled separately, the generation of the program only need to be the target program once connected to be able to. For example, in a file defined a function void a (), and the other file only the declaration of void A (), so does not affect the compilation of this file into the target file, when a file cannot find the definition of void a (), then put the function symbol in the symbol table, Wait until the connection to find the definition of void A () in other files. This compilation mode allows us to define functions in a file, so if you want to use this function, just declare it before you use it. The problem comes, if there are a lot of functions need to use, according to the method mentioned earlier, need to declare the function in this file, if many files need to use these functions, always can not be repeated definition, so the introduction of the header file. As can be seen from here, the header file is made for the declaration.

Header file: The contents of the header file and the source file are C + + source code, which is stored in the declaration, but the header file will not be compiled, in the pre-processing of Xxx.cpp compilation, #include <...> will be replaced, just text replacement, with a macro replacement, Also clear some extraneous code, such as comments.

/*math.h*/int Fx (int x); int Fy (int y);

/*math.cpp*/int Fx (int x) {    return x;} int Fy (int y) {    return y;}

/*main.cpp*/#include "math.h" int main () {    return 0;}

Math.cpp and main.cpp can be compiled separately MATH.O and MAIN.O, the final connection, you can get a complete program.

The contents of the header file: The header file should only put functions and variable declarations, can not be defined, because the header file will be more than one source file, the declaration can be more than one copy, but the definition of the function can only have a share, although the function definition of the content is the same, but does not conform to the specification. Definitions based on functions and variables can only be supported once, and will be discussed in detail.

1. The header file provides a centralized location for the declaration, which generally contains the definition of the class, the definition of the enumeration, the declaration of the extern variable, the declaration of the function, the definition of the const object, the definition of the inline function, and the appropriate other header file;

2. The header file can only have declarations, there can be no definition, there are several special cases;

(1) Const object: the definition of a const object can be written in a header file. Because the global Const object defaults to no extern declaration, it is only valid in the current file. Write such an object into the header file, even if it is contained in many other. cpp files, this object is only valid in the file containing it, and is not visible to other files, so it does not result in multiple definitions. At the same time, because the objects in these. cpp files are included in a header file, this ensures that the value of this const object in these. cpp files is the same, which can be described as double benefit. Similarly, the definition of a static object can be put into a header file. In most compiler implementations, the compiler replaces the use of these const variables with the corresponding constant expressions. Therefore, there is no storage space in practice to store const variables initialized with constant expressions. If a const variable is not initialized with a constant expression, it should not be defined in the header file. It should be defined in a source file and initialized in the same way as other variables, and an extern declaration is added to the header file to be shared by multiple files. (2) inline function: The inline function requires that the compiler, where it is encountered, expands it inline according to its definition, rather than a normal function that declares the relink (the inline function will not be linked), so the compiler needs to see the full definition of the inline function at compile time. In C + +, an inline function can be defined more than once in a program, as long as the inline function appears only once in a. cpp file, and in all. cpp files, the inline function is defined the same, and can be compiled. So in order to avoid repeating writing, the inline function is written in the header file.

(3) class definition: When creating an object of a class in a program, the compiler can only know how the class's objects should be laid out when the definition of the class is fully visible, so the requirement for the definition of the class is essentially the same as the inline function. In general, we do this by placing the definition of the class in the header file, and putting the implementation code of the function member in a. cpp file. It's OK, and it's a good idea. However, there is another way. That is to write the implementation code of the function member directly into the class definition. In a C + + class, if a function member is defined in the definition body of a class, the compiler will consider the function inline. Therefore, it is legal to write the definition of a function member into the class definition body and put it in the header file together. Note that it is not legal to write the definition of a function member in the header file of a class definition, but not in the class definition, because the function member is not inline at this time. Once the header file is contained by two or more. cpp files, the function member is redefined.

(4) Enum type: The enumeration type is equivalent to a const object and can be placed directly in the header file.

(5) Appropriate other header files: At this point, to minimize the dependency on other header files, when a header file is included also introduced a new dependency (dependency), just to the header file is modified, the code will be recompiled. If your header file contains other header files, any changes to the header files will also cause the code that contains your header files to be recompiled. 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 declaration, the header file needs to be pre-declared class file; No # include "File.h" is required.

How to use Class Foo in a header file without having to access the definition of the class:

1) Declare the data member type as Foo * ring Foo &;

2) The function of parameter, reusing return value type Foo is only declaration (does not define 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, the return-to-sample approach can reduce code readability and execution efficiency. If you just want to include the header files, do not replace the good.


Add:

1. These entities are defined in the header file because the compiler needs their definitions and not just declarations to produce the code.

2. Header File Protection: Use #ifnedef ... #define ... #endif进行头文件保护 to prevent duplicates from being included.

3. Directly in the file using the outer join entity, an extern declaration is made directly to the external connection entity (if the outer join entity type is a function, the extern keyword is not used).

4. Global variables, global functions are outer joins by default. Class definitions, global constants, const objects (variables), typedef types, and macro definitions are in-connection by default. In addition, global variables and global functions that are displayed with the static keyword when defined, have file scopes, and connection types are also internal. The default is an inner connection identifier, which, if displayed with the extern keyword, becomes an outer join.


The contents of the C + + programming header file and the source file

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.