Introduction to C ++ header file rules

Source: Internet
Author: User

The C ++ header file in C ++ serves as a bridge and link between your applications and function libraries. In the entire software, the header file is not the most important part, however, it is an indispensable part of the C ++ language family and provides great convenience to technicians.

Because the content of a header file is actually introduced into multiple different. cpp files, and they will be compiled. It is okay to put the Declaration. if the definition is put, it is equivalent to setting a symbolic variable or function in multiple files, even though these definitions are the same, however, this is not legal for the compiler.

Therefore, we should remember that, in the. h header file, only declarations of variables or functions can exist, rather than definitions. That is, sentences such as extern int a; and void f (); can only be written in the C ++ header file. These are declarations. If you write a sentence such as int a; or void f () {}, the compiler will immediately report an error once the header file is contained by two or more. cpp files. We have discussed extern before. Here we will not discuss the difference between definition and Declaration .)

However, this rule has three exceptions.

1. The definition of the const object can be written in the header file. Because the global const object does not have the extern declaration, it is only valid in the current file. Write such an object into the header file, even if it is contained in multiple other objects. in the cpp file, this object is only valid in the file that contains it. It is invisible to other files, so it will not lead to multiple definitions. At the same time, because the objects in these. cpp files are all contained in a header file, this ensures that the value of this const object in these. cpp files is the same, which can be two birds with one stone. Similarly, the definition of the static object can also be put into the C ++ header file.

Ii. Definitions of the inline function can be written in the C ++ header file. Because the inline function requires the compiler to expand it inline according to its definition when it encounters it, rather than the inline function that can be declared first and then chained without link ), therefore, the compiler must see the complete definition of the inline function during compilation.

If an inline function can only be defined once like a common function, this is hard to do. Because it is okay in a file, I can write the definition of the inline function at the very beginning, so that the definition can be seen later. However, what if I still use this function in other files?

This is almost no good solution, so C ++ stipulates that inline functions can be defined multiple times in the program, as long as inline functions are in one. the cpp file appears only once and. in the cpp file, this inline function has the same definition and can be compiled. Obviously, it is wise to put the inline function definition into a header file.

3. class can be written in the header file. When creating a class object in a program, the compiler can know how to layout the class object only when the class definition is completely visible, the requirements for Class Definition are basically the same as those for inline functions.

Therefore, it is a good practice to put the class definition into the C ++ header file and include the C ++ header file in the. cpp file of this class. It is worth mentioning that the class definition contains data members and function members.

Data members are defined to allocate space when a specific object is created. However, function members must be defined at the beginning, this is what we usually call the implementation of classes. Generally, we put the class definition in the header file, and put the implementation code of function members in A. cpp file. This is acceptable and a good solution.

However, there is another way. That is, the implementation code of function members is directly written into the class definition. In the C ++ class, if a function member is defined in the class definition body, the compiler will regard this function as inline. Therefore, it is legal to write the definition of function members into the class definition body and put them together in the header file.

Note that it is illegal to write the definition of function members in the class definition C ++ header file but not in the class definition, because this function member is not inline at this time. Once the header file is contained by two or more. cpp files, this function member is redefined.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.