Header and source files in C + +

Source: Internet
Author: User
Tags class definition

One, C + + compilation mode

Typically, in a C + + program, only two types of files--cpp files and h files are included. CPP files are referred to as C + + source files, which put the source code of C + +, h file is called C + + header file, which is also the C + + source.

c+ + language supports "compile separately" (separate compilation). That is, all the content of a program can be divided into different parts in different. cpp files.

CPP files are relatively independent, in the compilation (compile) do not need to communicate with other files, only need to compile into the target file and then the other target file to do a link (link) on the line. For example, a global function "void A () {}" is defined in file A.cpp, and this function needs to be called in file B.cpp. Even so, file a.cpp and file b.cpp do not need to know each other's existence, but can be compiled separately, compiled into the target file after the link, the entire program can be run.

Second, C + + header file

The contents of the header file are the same as the contents of the. cpp file, which is the C + + source code. But the header file does not have to be compiled.

We put all of the function declarations into a header file, and when a CPP source file needs them, they can be included in the CPP file by a macro command "#include" to merge their contents into the CPP file. When the CPP file is compiled, the role of these included H files is played.

#include的作用是把它后面所写的那个文件的内容, complete the whole place, the word does not change to include in the current file. It is worth mentioning that it has no other function or function, and its function is to replace every place where it appears, replacing it with the content of the file written behind it. Simple text substitution, nothing else. Therefore, the first sentence in the main.cpp file (#include "math.h") is replaced with the contents of the Math.h file before compiling. That is, when the compilation process is about to begin, the content of main.cpp has changed.

Third, the writing of the head file

1. In the header file, only the declaration of the variable or function can exist, not the definition.

Because the contents of a header file are actually introduced into several different CPP files, they are all compiled. It's okay to put a statement, if you put a definition, it's equivalent to having a definition of a symbol (variable or function) in multiple files, even though the definitions are the same, but for the compiler, it's not legal.

2. The definition of a const object can be written in the header file.

Because the global Const object defaults to no extern declaration, it is only valid in the current file. Writing such an object into a header file, even if it is contained in multiple CPP files, is only valid in the file containing it, and is not visible to other files, so it does not result in multiple definitions. Also, because the objects in these CPP files are included in a header file, this ensures that the value of the Const object in these CPP files is the same, which is double benefit.

Similarly, the definition of a static object can be put into a header file.

3, the header file can write the definition of inline function (inline).

An inline function is a compiler that needs to see the full definition of an inline function at compile time by requiring it to be expanded in the context of its definition, rather than as a normal function that declares the relink (inline functions are not linked).

If the inline function can only be defined once as a normal function, this is a difficult thing to do. Because it's OK in a file, I can write the definition of the inline function at the very beginning, so that you can see the definition when you use it later, but what if I use the function in other files? There is hardly a good solution.

As a result of 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. Obviously, it is wise to put the definition of an inline function into a header file.

4. You can write the definition of class in the header file.

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. Therefore, it is a good practice to put the definition of a class into a header file and use it to include this header file in the. cpp file of this class.

It is worth mentioning that the definition of a class contains data members and function members. Data members are not defined until a specific object is created (allocating space), but function members need to be defined at the outset, which is what we typically call the implementation of the class.

The general practice is to put the definition of the class in the header file, and put the implementation code of the function member in a CPP file . That's a good way to do it.

There is another way to write the implementation code of a 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 than two CPP files, the function member is redefined.

5, the first file needs protection measures

If the header file contains only declaration statements, it is not a problem to be included in the same CPP file more than once. But if the header file is some of the above, define some functions, the CPP file contains two times the header file is troublesome.

Using the #define MATE condition compilation can be a good solution to this problem. In a header file, a name is defined by a # define, and the conditional compilation #ifndef ... #endif使得编译器可以根据这个名字是否被定义, and then decide whether you want to continue compiling the subsequent contents of the header. This method is simple, but you must remember to write the header file.


Reprinted from: http://www.cnblogs.com/lidabo/archive/2012/04/17/2454568.html

Header and source files in C + +

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.