Class Forward Declaration, the difference between class and include

Source: Internet
Author: User

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



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



Foo foo_test (foo &);


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.