About header file dependencies for C + +

Source: Internet
Author: User

is looking at the Google C + + programming specification, the inside of the enemy file dependency is said:

Use the predecessor Declaration (forward declarations) to minimize the number of # include in. h files. When a header file is included, a new dependency (dependency) is introduced, and the code is recompiled as long as the header file is modified. If your header file contains other header files, any changes to these header files will also cause the code that contains your header files to be recompiled. Therefore, we would prefer to include as few header files as possible, especially those contained in other header files. Using a predecessor declaration can significantly reduce the number of header files that you need to include. Example: The header file uses the class file, but does not need to access the file's declaration, the header file only need to pre-declare the class file, do not need to include "file/base/file.h". How does the header file do with class Foo without having to access the definition of the class? 1) Declare data member types as Foo * or foo &;2) parameters, functions that return a value of type Foo are just declarations (but do not define implementations); 3) The type of a static data member can be declared as Foo, because the definition of a static data member is outside the class definition. On the other hand, if your class is a subclass of Foo, or contains a non-static data member of type Foo, you must include the header file for it. Sometimes it is more meaningful to substitute object members (object member) with pointer members (pointer, if SCOPED_PTR is better). However, this approach can reduce code readability and execution efficiency. If you just want to include the header files, do not replace the good. Of course, the. cc file requires a defined portion of the class that is used anyway, and it will naturally contain several header files. Note: You can rely on the declaration to not rely on the definition.

So under the VC to do the next Test, the first self-established foo.h,foo.cpp file, the definition of a class cfoo, no tube How to define, any line, and then suggest Main.cpp, in the inside test:

Class Cfoo;class test{public:/*1*/cfoo func (cfoo foo);//ok/*2*/static Cfoo sub;//ok/*3*/cfoo& foo1;//ok/*4*/CFoo* pfoo1;//ok/*5*/Cfoo foo; Error};

Before are directly include Foo.h file, this time do not reference, but declare the Cfoo class. See the Declaration of the test class below.

1, correct. Describes the 2nd in the specification, the function parameter and the return value are only declarations.

2, correct. The 3rd in the specification, the type of a static data member can be declared as Foo, because the definition of a static data member is outside the class definition.

3, 4, correct. 5 error. The specification of the 1th, non-static data members must be pointers or references, otherwise need to rely on the definition, in fact, it is understood, non-static means that each instance occupies memory, it is necessary to know the size of memory, no definition how to know?

About header file dependencies for C + +

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.