Differences between description and definition

Source: Internet
Author: User

Generally speaking:StatementIs to tell the compiler that such an identifier exists.DefinitionIt applies for a piece of memory for the program.

For example:

 
 
  1. Int x; // This is a definition
  2. Extern int x; // This is the Declaration
  3. Int x = 10; // This is also a definition

Use extern int x = 10 in the program, which is also defined.

Declared operations are generally used in header files, so that the same variable can be referenced in multiple source files. This also shows why definition cannot be used. Because the header file is actually a part of the source file after it is included in the source file.

Therefore, if a variable is defined in the header file, the variable definition will appear in multiple source files. That is, repeated definitions.

As mentioned above, variables cannot be defined in the header file. However, there are three exceptions. The header file can define classes, const objects and inline functions that are known at compilation.

Define these entities in the header file because they are not just declared) because the compiler needs their definitions to generate code.

For example, to generate code that can define and use class objects, the compiler needs to know the data members that make up the class and the operations that can be performed on these objects. The class definition provides this information, so you need to define the class in the header file.

Because the const variable is the local variable that defines its file by default. Therefore, the definition of this variable is valid even in multiple source files.

1. When the const variable is initialized using a constant expression, its initialization process can be implemented in the header file.

2. If the const variable is not initialized using a constant expression, the variable cannot be initialized in the header file, but must be defined and initialized in the source file. Add the extern declaration to the header file so that it can be shared by multiple files.
 

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.