Definition of the Forward Declaration: Sometimes we can declare some classes but not define them. Of course, the function of this class is also very limited.
For example, class foo;
Declare a foo class, which is also called the forward declaration (forward declaration). After declaring the foo class, define the period before the foo class, the foo class is an incomplete type (incomplete type), that is, the foo class is a type, but some properties of this type (such as which members are included and which operations are available) are unknown.
Therefore, this class has limited functions.
(1) Objects of the foo class cannot be defined.
(2) It can be used to define a pointer or reference to this type. (Something of great value)
(3) A function used to declare (not defined) the type as a form parameter or return type.
Because of the existence of forward statements, we can easily do some things in many cases.
In c ++, if you want to write a header file for the class, you generally need to # include a pile of header files, but use the Forward Declaration and the features of the c ++ compiler, most of them are not required.
The c ++ compiler mainly performs the following tasks: 1. Scanning symbols; 2. Determining the object size.
Therefore, you do not need to include all classes.
For example:
(1) Since all object types of applications occupy the same large space, the c ++ compiler is very good at verifying the object size.
Class string;
Class Sample
{
Private:
String & s;
};
Here, you only need to make a forward declaration of a string. # include <string> is not required.
(2) because all types of pointers are of the same size. So similar to (1), you can only make the Forward Declaration.
(3) Declare the form parameter or return type of the member function, or use the nature of the Forward Declaration.
Class string;
Class foo;
Class Sample
{
Public:
Foo foo_test (foo &);
Private:
String & s;
Foo * f;
};
Here, I have not defined a foo class, but it can still be used like this. Because the member functions do not occupy the class object size, the c ++ compiler can determine the object size.
The purpose of Forward Declaration is to tell the compiler A type defined elsewhere. In this way, the C ++ compiler can generate the correct symbol table.