Questions from csdn:
I always thought that definition = Declaration + initialization, that is, int I; Is declaration, int I = 0; Is definition.
But according to Hou Jie's TranslationValid C ++The string PS is also a definition (in the section where the clause definition should be as backward as possible). if the definition is too early, the constructor burden will be introduced too early.
Have I ever been wrong with the concepts of declarations and definitions?
If the Declaration does not cause memory allocation, how can we differentiate definitions and declarations?
I have selected several reliable answers in my reply downstairs to summarize them, in case I forget them in the future.
The rigorous C ++ semantics of the statement is used to tell the compiler type and its details, for example:
ClassMyclass
{
// Data member details...
// Member function details...
};
The above statement only tells the compiler that there is a custom type of myclass. The Compiler only performs vocabulary analysis and name resolution on it, and does not occupy the memory!
The strict C ++ semantics of "Definition", that is, memory occupation. The compiler will address the object in the relative memory address!
Note that we cannot simply say
StringMystring;
Whether it is a declaration or definition. The determination principle is to check whether the memory is occupied. For example:
ClassMyclass// Class declaration, no memory occupied{StringMystring;// String Declaration};
However
# Include <iostream>
// Global scope
StringMystring;// Definition. mystring is the instantiated string!
IntMain ()
{
// Main function body
StringMyanotherstring;// Definition. myanotherstring is the instantiated string!
Return0;
}
Therefore, I replied:
Variables and objects without extern are always defined, except in classes.
Only function headers are declarations, and function bodies are definitions.
Class is always a declaration. The function body of a class member function is defined.
ClassMyclass
{
Static IntX;// Here, X is the Declaration
Static Const IntA;// Here a is the statement
// Non-static variables are allocated during class instantiation.
Myclass ();// The function here is the Declaration
};
IntMyclass: X;// This is the definition
Const IntMyclass: A = 11;// This is the definition
How is the distinction made? Hope you can answer your questions.
Original post: Click here!