C++ header file and implementation file details

  • 2020-04-02 02:58:10
  • OfStack

In C++ programming, as the project grows larger, the code grows larger and harder to manage and analyze. Therefore, in C++, we have to separate the header (.h) file and the implementation (.cpp) file, and we also have the concept of Package.

Starting with C and learning C# as my "native language", I was still confused about this aspect when I started to learn C++ from my tutor. Although I can use the knowledge of C to understand the syntax of C++ and use the thought of C# to understand the use of C++ classes. However, the definition and implementation in C# are all in one file (in fact, they are all in the class), and the use of C is just the beginning of programming, writing the program is only one file is enough. Therefore, there is always a struggle to understand the Package of C++ as well as the.h and.cpp files.

Fortunately, the tutor has a detailed PPT to let me know, once the understanding of the Package will be more clear. In short, a Package consists of.h and.cpp files of the same name. Of course, any one of these files can be missing: only the Package of the. H file can be the definition of the interface or template; Only the Package of the.cpp file can be the entry to a program.

Of course, more detailed explanation, welcome to download the tutor's teaching PPT-Package to learn more.

But what I want to talk about here is the.h file and the.cpp file

Knowing the Package is only a relatively broad understanding: we use the Package as the editing object in the project to extend and fix our program. I was confused again when I wrote the code to specify what to put in the.h file and what to put in the.cpp file.

Although Google gave me a lot of links, most of the explanations were too general: the declaration was written in. H, and the definition implementation was written in. CPP. There is nothing wrong with this explanation, but when you do start, you will find that you don't know where to type the code.

So I turned the problem over to my tutor, who patiently explained to me how to do code separation in C++ in great detail. Unfortunately, the first time I listened, but did not understand too much, and was not deep in C++, so I did not have a deep impression.

After several projects and trials, I took out the question and asked the instructor, who patiently explained it to me again (I swear he never forgot I had asked the same question), and this time I wrote it down.

In order not to forget, I summarize them here.

An overview of

  Non-template type (none-template) Template type (template)
The header file (.h)
  • Global variable declaration (with) extern Qualifier)
  • Declaration of global functions
  • with inline qualifiers The definition of global functions
  • The definition of a class
  • Declaration of class function members and data members (inside the class)
  • The function definition within the class definition (equivalent to inline )
  • with static const qualifiers Data members in Inside the class The initialization
  • with inline qualifiers Function definition outside of the class definition
  • The definition of a template class
  • Declaration and definition of a member of a template class (the definition can be placed in or out of the class, and there is no need to write outside the class inline )
Implementation file (.cpp)
  • Definition (and initialization) of global variables
  • Global function definition
( There is no )

* declaration: declaration
* definition: definition

The header file

All contents of the header file must be included in

# # ifndef {Filename}
# define {Filename}

/ / {the Content of the head file}

# endif
This ensures that when the header file is referenced by more than one other file, the internal data is not defined multiple times and causes errors

The inline qualifiers

In the header file, you can tell the compiler by using the inline qualifier on the function, which is very simple and can be embedded directly into the call definition.

Of course, the inline function is not necessarily implemented by the compiler as inline, and the compiler will reject inline if the function is too complex.

So simply put, it is best to use inline as short as 3-5 lines. Do not use inline for functions with loops, branches, and recursions.

For functions that are defined within the class definition, the compiler automatically treats the inline request as inline (which is not always inline). So below, I call the function members with the inline qualifier and the function members written in the body of the class definition "the function members to inline"

Non-template type

Global type

Just like the general statement above: the statement is written in the.h file.

For functions, a function without an implementation is equivalent to a declaration; For data types (both primitive and custom), the declaration needs to be decorated with extern.

These global functions and variables are then defined, implemented, or initialized in the.cpp file.

However, the instructor has repeatedly stated that global functions and variables are not to be used. The consequence that caused after used, at present is the homework item that hand in will deduct a mark. Certainly not with the reasons and solutions that are not available, but not in the current discussion.

Custom type

For custom types, including classes and structs, they are defined in. H files. The declaration and definition of its members is more complicated, but look at the above table, or more clear.

Members of the function

Function members are declared inside the class definition of the.h file, whether or not they have a static qualifier.

For the function member to inline its definition is placed in the.h file; The other functions are implemented in the.cpp file.

Data members

The declaration and definition of data members are placed inside the class definition in the.h file. With data types, the key question is where to initialize them.

For a data member with only the static qualifier, its initialization is in a.cpp file. Because it is common to all class objects, it must be properly initialized.

For a data member that contains only the const qualifier, its initialization can only be done in the constructor's initialization list. Because it cannot be re-assigned once it is initialized, it must also be properly initialized.

For a data member that contains both a static qualifier and a const qualifier, it is initialized and defined at the same time. It must also be properly initialized

For a data member that has neither a static qualifier nor a const qualifier, its value can be changed at will only for the object, so we don't care when it is initialized.

Template type

Templates are a powerful tool for development in C++. They are similar to, but not identical to, C# and Java generics. I've always thought of generics and templates as things that I might never need to use in my life. However, under the compulsion of my tutor, I really realized the power of templates, and really knew how to use them and how to design them. However, this is not a few words can be finished, I will not say more.

The most important thing about a template is that the compiler does not compile it when it is defined because it has no entities available.

Only after the template is specific (specialization) (with) on the specific type, the compiler will only depending on the type of template to compile.

So when I defined the template, I found that the compiler rarely reported errors (I was happy: I wrote code that was error-free) and couldn't do a smart tip. However, when it is applied to a specific class, the errors will appear in large areas, but often can not be accurately located.

Therefore, the design template has a set of design template ideas and ways, but this is also deviated from the theme of this article.

Because of the specificity of the template, it doesn't have its own definition, so we can't put it in a. CPP file, but all of them in an. H file. This is also to allow the compiler to find out where all the definitions of the template are when the template is externalized so that the actual method can be defined.

As for where to put the definition of the function members of the template class, the advice of the tutor is to put it outside of the class definition, because then when you look at the class, you will clearly know what methods and data are available. When I was using Visual Studio, I looked at the implementation of its standard library, which was all inside the class.

Perhaps because I'm used to the C# style, I prefer to write them all inside classes, and because the editors I use during development have one powerful feature: code folding.

The other reason, of course, is that it is written outside of the class. For each function member, the template type is written as a qualifier, and the class name qualifier is written as a qualifier.


Related articles: