Recently, when I was working on a project, I found that some features close to the underlying operating system were not powerful, and many third-party things were used in the project, A large part of my work is dealing with hardware and I think it is necessary to review c ++ (abandon "she" for three years). I think it is better to return to it!
This trip records the beauty I have lost! Don't talk nonsense. Write it!
Concept Review: "Declaration" and "Definition"
The two concepts must be explained. First, we must know the difference between "Declaration" and "Definition", because these two terms often appear in our usual talking, it is the responsibility to understand them.
"Declaration" introduces the name to the computer and says, "What does this name mean ".
"Definition" assigns a bucket for this name.
Both variables and functions have the same meaning. In either case, the compiler allocates storage space in the "definition. For variables, the compiler determines the number of storage units occupied by the variables and generates space for them in the memory. For a function, the compiler generates code and allocates storage space for it. A function bucket contains a pointer generated by a function name without a parameter table or an address operator.
In addition, the definition can also be declaration. If the compiler has not seen the name A, and the programmer defines int A, the compiler immediately allocates A storage address for the name. Declarations often use the e x t e r n keyword. If we declare a variable rather than define it, we need to use e x t e r n. For function declaration, e x t e r n is optional. The function name without the function body, together with the parameter table or return value, is automatically declared.
The function prototype includes all information about the parameter type and return value. Int f (float, char); is a function prototype, because it not only introduces the name of the function f, but also tells the compiler what parameters and return values this function has, this allows the compiler to properly process parameters and return values. C ++ requires that a function prototype be written because it adds an important security layer.
The following are some declaration examples.
// Declare Variables
Extern int varible;
// Declare a function
Extern float foo (float, float );
Float B;
Float c;
// Define a function
Float foo (float a, float B)
{
Return a + B;
}
// Define Integer Variables
Int varible;
// Define a function
Int h (int x)
{
Return x + 1;
}
Void main (int argc, _ TCHAR * argv [])
{
Varible = 2;
B = 1.0;
C = 2.3;
Foo (B, c );
H (varible );
}
The parameter name can be provided or not given during function declaration. They are required in definition. This is true in C, but not necessarily in C ++.
In fact, it is very simple. In summary, the Declaration does not allocate memory space. The definition is opposite. The definition can be declaration, but the Declaration must not be definition!