Google C + + Programming specification – 19th – "Pre-declaration"

Source: Internet
Author: User

Transferred from: http://roclinux.cn/?p=3285

This original article belongs to the "Linux Greenhouse" blog.

The blog address is http://roclinux.cn.

The author of the article is Roc Wu

==

Specification

For normal classes, it is recommended to use a predecessor declaration instead of # include.

"What is a predecessor statement"

In English, a predecessor declaration, called "forward declaration", refers to "declaring a class, function, or template without related specific definitions". We can use the predecessor declaration instead of the # include statement for the declaration.

"Voice of the Supporters"

Too many # include causes the compiler to open many files and process a lot of input. In addition, when the head file content is modified, it also causes more recompilation.

If we use the predecessor declaration, we can avoid the above problem.

"The voice of the opponents."

    1. In the use of templates, typedef, default parameters and other syntax, we are more difficult to determine the correct form of the predecessor Declaration;
    2. For a piece of code, it is difficult to decide whether to use a pre-declaration or an # include, especially if there is an implicit conversion operation. In some extreme cases, replacing # include with a predecessor declaration completely alters the original intent of the code.
    3. If you want to pre-declare too much content, it will appear redundant procrastination, as easy as # include.
    4. The pre-declaration of functions and templates prevents the original author from improving the function or template, such as adjusting the type of a parameter, adding a default value to the parameters of the template, and so on.
    5. The namespace std:: The pre-declaration of the tag, often produces an "undefined" error.
    6. If you deliberately tweak your code to use a predecessor declaration, you tend to slow down your code and complicate the logic of your code, such as using pointer members instead of object members.
    7. The benefits of the pre-statement to code efficiency are not being proven.

Conclusion

    1. If you want to use a function in the header file, use the # include method to reference the header file
    2. If you want to use a class template from a header file, use the # include method to reference the header file
    3. If you want to use a generic class, it is recommended that you use a predecessor declaration.
    4. You do not need to substitute pointer members for object members just to avoid using # include.

Thank you!

Google C + + Programming specification – 19th – "Pre-declaration"

Related Article

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.