C + + header file definition rules

Source: Internet
Author: User

The header file for C + + provides a centralized location for related claims. The header file generally contains the definition of the class, the declaration of the extern variable, and the declaration of the function.

Because the header file is contained in more than one source file, you should not include the definition of a variable or function. Otherwise, there will be redefinition errors when linking.

But there are three exceptions: the header file can define the class, the const object and the inline function that the value is known at compile time. These entities can be defined in multiple source files, as long as the definitions in each source file are the same.

declarations and definitions in C + +

1. Variables in C + + must be and can be defined only once, and variables must be defined or declared before they are used .

2. int i; Defines a variable i. You can declare a variable name without defining it by using the extern keyword, extern int i, declaring the variable i. The extern declaration is not defined, nor does it allocate storage space, in fact, it simply illustrates that the variable is defined elsewhere in the program. However, if the declaration has an initialization, it is treated as defined, even if the declaration is marked extern.

3. In summary, variables used in multiple files can be declared in a header file, defined in a source file, and other files that use the variable contain the header file that declares the variable.

C + + header file definition rules

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.