Forward declarations and # include differences for the basic (20) C + + base class

Source: Internet
Author: User

Introducing another class into a header file is often written in two ways:

(1) class Name;

(2) #include "Name.h"


What is the difference between them?

The first one is called forward declaration, name is an incomplete type (incompete Type), known as name is a type, but does not know which members are included. an incomplete type can only be used in a limited way, cannot define an object of that type, the incomplete type can only be used to define pointers and references to that type, or to declare (rather than define) a function that uses that type as a formal parameter type or return type. (The forward declaration only knows that there is one class, but the specific information is unknown); In addition, it can Eliminate compilation dependencies and reduce the time it takes to compile (if # Include "Name.h" once the definition of Name needs to be modified, Name.h will be recompiled, Causes all files that use Name.h to be recompiled, which results in a compilation dependency, which increases the time to compile, and, in large projects, it can even significantly increase compilation time if there are many such places. )


The second include header file associates the entire header file with where it is used, so you can know the specific information about the class. at the time of compiling the Name.h file directly, so the interface can be used, you can declare the object, call method.



Forward declarations and # include differences for the basic (20) C + + base class

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: info-contact@alibabacloud.com 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.