Definition of a forward declaration: Sometimes we can declare some classes but not define them, and of course this class has a limited role to play.
such as Class Foo;
Declare a Foo class, which is sometimes referred to as a forward declaration (Forward declaration), after declaring the Foo class, defining 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 the type, such as which members are included, and what actions are not known.
So the role of this class is also very limited.
(1) The object of the Foo class cannot be defined.
(2) can be used to define pointers or references to this type. (something of great value)
(3) Used to declare (not define) a function that uses the type as a formal parameter or return type.
Because of the existence of forward declarations, we can do things easily in many cases.
In C + +, if you want to write a header file for a class, you typically include a bunch of header files, but most of the features of forward declarations and C + + compilers are not needed.
What the C + + compiler does is: 1. scan symbols; 2. Determine the size of the object.
So there are times when you don't need to include classes in.
Like what:
(1) Because references to all object types occupy the same amount of space, the C + + compiler is good at confirming object size.
class string ; class sample{ private: string &s;};
Here you just need to make a string forward declaration, you don't need # include <string>
(2) because all types of pointers are also of the same size. So, like (1), you can just make a forward statement just fine.
(3) declaring the parameter of a member function is either the return type or the nature of the forward declaration.
class string ; class foo; class sample{public: &); Private : string &s; *F;};
Here, I do not define an Foo class at all, but it can still be used because member functions do not account for the size of the class object, so the C + + compiler can still determine the size of the object.
The purpose of a forward declaration is to tell the compiler that this is a type that is defined somewhere else. This allows the C + + compiler to generate the correct symbol table.
Forward declarations for C + + classes