What should be placed in the header file? What is external connection? ---- Reprint

Source: Internet
Author: User

Http://www.cnblogs.com/younes/archive/2010/04/13/1711305.html

 What should be put in the header file?

The header file provides a centralized storage location for related declarations. The header file generally contains the class definition, enumeration definition, extern variable declaration, function declaration, const int definition, and inline function definition. Files that use or define these entities must contain the appropriate header files.

Header files are used for declaration instead of definition.

Remember the differences between definitions and declarations when designing header files. The definition can appear only once, but the Declaration can appear multiple times. The following statements are definitions, so they should not be placed in the header file:

Extern int ival = 10; // has initialization, so it is defined
Double fica_rate; // No extern keyword, so it is also defined

Because the above two statements are defined (in fact, because the default connection type of global variables is extern, that is, external connections, when two or more files in the same program contain any of the preceding definitions, multiple definition links may be incorrect.
Because the header file is designed to be contained in multiple source files, it should not contain the definition of variables or functions.

Header files should not contain the definition rule. There are three exceptions: class definition, enumeration definition, const object known at compilation, and inline function. These entities can be defined in multiple source files, as long as the definitions in each source file are the same.

Define these entities in the header file because the compiler needs their definitions (not just declarations) to generate code. For example, to generate code that can define and use class objects, the compiler needs to know the data members of this type. You also need to know the operations that can be performed on these objects. The class definition provides such information.

By default, a const object (variable) is a local variable of the file that defines the variable. The connection type is inner join. Therefore, when multiple source files include the same header file containing the definition of the const object, there will be no Link errors for multiple definitions. This is because, in essence, each source file that contains this header file has its own const variable, which is allocated to it with the same name and value. At the same time, because the default connection type of the const variable is internal connection, there will be no multi-definition Link errors.

In most compiler implementations, the compiler replaces the use of these const variables with corresponding constant expressions. Therefore, in practice, no storage space is used to store the const variable initialized with a constant expression.

If the const variable is not initialized using a constant expression, it should not be defined in the header file. It should be the same as other variables, should be defined in a source file and initialized, add the extern Declaration for it in the header file to be shared by multiple files.

External Connection

Global variables and global functions are connected by default.

Internal Connection

Class Definition, global constant, const object (variable), typedef type, macro definition is inner join by default. In addition, global variables and global functions with the static keyword are displayed during the definition, and the connection type is internal connection.

By default, it is the identifier of the inner connection. If the extern keyword is displayed, it becomes an external connection.

Use of external connection identifier

When using external connected entities, You can include the file where the definition is located (incorrect method, resulting in chained errors: multiple definitions), or include the file where the declaration is located. You can also use the third method: directly connect to an external object in a file that uses an external connection object for an extern Declaration (if the external connection object type is a function, the extern keyword is not required, the function prototype is OK ).

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.