C++ declarations and definitions and the uses of header files and source files
Learn only
1. Declaration and definition
-
Declaration: TellCompilerThe name and type of a variable or function, but does not allocate memory. A declaration can be regarded as a notification to the compiler, telling it the type of an identifier. A variable or function can be declared multiple times, but can only be defined once.
-
Definition: is the process of creating an executable code entity that allocates memory andinitializationvariable orfunction. There can only be one definition, and it must appear somewhere in the program so that the compiler knows how to generate the corresponding code.
1.1 Variable Examples
(a) extern int ix = 1024;
(b) int iy;
(c) extern int iz;
(d) extern const int &ri;
- 1
- 2
- 3
- 4
1.2 Explanation
(a) extern int ix = 1024; This is a definition. externKeywordsIndicates that the ix variable may be defined elsewhere, but in this declaration, it is initialized, so it is a definition. Even though extern is usually used to indicate that a variable or function is defined elsewhere, it is still a definition if an extern declaration contains initialization.
(b) int iy; This is a definition. extern is not used here, and the variable iy is declared but not initialized, it will be defined in the file that declares it.
© extern int iz; This is a statement. Extern is used here and is not initialized, so it tells the compiler that the iz variable is defined elsewhere, and here it is just declared its type.
(d) extern constint &ri; This is a statement. A reference type const int & is declared here and extern is used, meaning that the constant integer variable pointed to by this reference is defined elsewhere. Since there is no initialization, it is a declaration.
2. Header files and source files have different uses
-
Header files: Usually used to declare, that is, tell the compiler the name and type of variables or functions, but do not define them. The header file is contained by multiple source files to share interfaces and declarations.
-
Source file: Used to define, i.e., to actually create variables or functions and provide implementation details.
2.1 Variable examples
(a) int var;
(b) const double pi = 3.1416;
(c) extern int total = 255;
(d) const double sq2 = sqrt(2.0);
- 1
- 2
- 3
- 4
2.2 Explanation
(a) int var; This is a variable definition that should be placed in the source file. Because it allocates memory, there can only be one definition. If placed in a header file, each source file containing the header file will try to define it, resulting in a duplicate definition error.
(b) const double pi = 3.1416; This is a definition because it initializes the constant. This definition should be placed in the source file. However, if pi is a constant value and is used in multiple places, it can be placed in the header file as an inline variable to avoid duplicating the same value in each source file using it.
© extern int total = 255; This is a definition because extern declares an externally linked variable, and initialization is also provided here. Since extern is usually used for declarations, but when it is used with initialization, it actually creates a definition. This definition should be placed in the source file, and only one source file should contain this definition to avoid duplicate definitions.
(d) const double sq2 = sqrt(2.0); This is a definition because it initializes a constant. However, this initialization relies on a function call sqrt, which makes it impossible to put in the header file, because the function call cannot be initialized as an inline variable. This definition should be placed in the source file.