Definition and declaration definition, declaration
Definition: Fully defining entities in a declaration, often requiring allocating memory space
Declaration: a simple Declaration entity.
Most declarations are defined in addition to the following scenarios.
extern-Modified, and no initialized variables
extern const int a; // declares, but doesn‘t define a extern const int b = 1; // defines b
function declaration without body of function
int f(int); // declares, but doesn‘t define f
Static member variable declaration in the class definition
struct S { // defines S int n; // defines S::n static int i; // declares, but doesn‘t define S::i }; int S::i; // defines S::i
class declaration (Forward declaration)
class S; // declares, but doesn‘t define S class Y f(class T p); // declares, but doesn‘t define Y and T (and also f and p)
Template parameter declaration
template<typename T> // declares, but doesn‘t define T
Template specificity
template<> class A<int>; // declares, but doesn‘t define A<int>
typedef
typedef S S2; // declares, but doesn‘t define S2 (S may be incomplete)
Defined, can only occur once (one definition Rule)
Any variable, function, class, template, Enumeration type in a compilation unit can be defined only once, but can be declared more than once.
- Non-inline functions and variables, throughout the program, can only be defined once (not just the compilation unit) (the entire program here, including the standard library and user-defined libraries).
- inline functions, in a compilation unit, can be defined only once.
- class, which can only be defined once in a compilation unit.
- Class type, enumeration type, inline function with external linkage, class template, non-static function template, static data member of a class template, member function of a class template, partial template specialization, meet certain conditions, can be in a compilation unit, fixed Righteousness once.
For a thing that can only be defined once in the whole program, we usually write it in a. cpp file, and, for a compilation unit that can only be defined once, we tend to write it in an. h file.
Note: As mentioned above, in a compilation unit can only be defined once, with a "meet certain conditions", this condition is more complex. However, if we write the definition in an. h file, this condition is usually met
What is the case that needs to have a definition
Simply put, an object must be defined if it needs to get its address, or if it has a reference bound to it, a function that must be defined if it is called by code. If a variable or function needs to be defined, however, it is not defined throughout the program, then the program generates a link error.
class A{public: static const int num=10; //declare num};int main(){ int a = A::num; //not need define A::num const int& ra = A::num; //need define A::num return 0;}
Strictly speaking,
How to tell if a variable needs to be defined
Variables appear in many expressions, and if one of the expressions needs to define the amount of change, the program needs to define the variable, otherwise it does not need to be defined.
The judgment variable x appears in the expression ex, does it need to define X?
If the following two conditions are met at the same time, you do not need to define X, otherwise, define X.
- An lvalue is converted to an rvalue on X, no other function is called, and a constant expression is generated.
An ex contains an expression of a potential computed x, either discarding its results, or doing lvalue-to-rvalue conversions.
struct S { static const int x = 1; }; int f() { return S::x; } // does not need define S::x
Or
struct S { static const int x = 1; }; void f() { &S::x; } // discarded-value expression does not need define S::x
How to tell if a function needs to be defined
You need to define a function in the following situations:
- If a function is called, explicit or implicit.
- Virtual functions, (non-pure virtual functions) (because virtual tables need to be built)
- If new appears (explicit or implicit), you need to define the allocation or deallocation function
- If Delete appears (explicit or implicit), you need to define the deallocation function
C + + declarations and definitions