Forward Declaration in C ++)

Source: Internet
Author: User

I want to go around, because I want to do a QT project and pick up C ++ again. This is a programming tool for the first language for me.

It was the first time that I was responsible for designing the project architecture. It was a bit excited and confused. But fortunately, I feel like I am coming over. The basic structure design document has been written and the basic engineering framework has been set up, and the formal coding phase will soon be available.

One of the gains of Writing module-class files today is that when defining header files, do not rely on definitions if they depend on the pre-declaration, at the same time, you should use the pre-declaration of the type to reduce the import of header files when appropriate. For example, when a pointer member of a class is defined in the header file, the pointer is another type defined in the project.Class
A pointer to an account.Account*
M_account ;. Add the pre-declaration before the class declaration of the header file:

 
Class account;

It is OK, and you do not need to include the header file of the Account:

 
# Include "account. H" // The pre-declaration replaces this import Method

What are the benefits of doing so?

You can use the pre-declaration to reduce the header files to be included. When a header file is included, a new dependency is introduced. As long as the dependent header file is modified,CodeIt will be re-compiled. This dependency will be recursively transmitted, that is, header file B containing header file a. When header file a is changed, header file B and all header files containing header file B will be re-compiled. Therefore, this situation should be appropriately reduced.

However, the pre-declaration of the type is conditional. Suppose there is a test class. If your class has a non-static member defining the test type, or your class inherits the test class, you must include the header file where test is located, but cannot use the pre-declaration of the test class. In three cases, you can use the test class instead of accessing the test definition body:

(1) parameter or function declaration with the return type of test;

(2) Static Class Members of the test type;

(3) declare a class member variable as test * or Test &;

Progress comes from summing up, warming up and updating. This is the truth ~

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.