C + + header file contains problem-include&class

Source: Internet
Author: User

http://blog.csdn.net/jiajia4336/article/details/8996254

Forward declaration Concept (forward declaration)

The class type B is introduced in the program. After the declaration, the class B is an incomplete type (incompete type), that is, B is known as a type, but does not know which members are included. An incomplete type can only be used in a limited way and cannot define an object of that type. A non-full 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.

Forward Declaration Application Scenario

When you need to define two classes or structures, such as a and B, and these two classes need to reference each other, when defining a, B is not defined, then how to reference it, this time requires a forward declaration (Forward Declaration), the forward declaration format is as follows: Class B; When you declare B before you define Class A, you introduce a class B in your program, and the compiler knows that you will define Class B somewhere in the future, so you are allowed to reference Class B in Class A. However, after the declaration, the class B is an incomplete type (Incompete type), which is known as a type, but does not know some of the properties of the type (such as which members and operations are included).

limitations on the use of forward declarations: (Take the above declaration of Class B description)

1. Can not define the object of Class B;

2. Can be used to define only the type of pointer or reference;

3. Used to declare a function that uses the type as a formal parameter or return type;

In addition to the above restrictions, what can we do with it?

In C + +, if you want to write a new class header file, usually to include a bunch of dependent header files, but the use of forward declarations and features of the C + + compiler, you can reduce the workload here.

Because the C + + compiler does the main thing is: 1. scan symbols; 2. Determine the size of the object. With this feature, when we write a new class header file, we can use forward declarations, reduce the number of # include, and reduce the compilation

Workload.

For example:

1.B class use to Class A, the operation is as follows, do not need to add A.h, reduce the compilation effort

1:

2://b.h

3:class a;//Call class A, forward declaration

4:class

5:b

6: {

7:private:

8:a *a; declaring pointers

9:};

2. When declaring a parameter or return type of a member function, you can also use a forward declaration

Even if we do not define an Foo class, it can be used because the member function does not occupy the size of the class object, the compiler can determine the size of the object, and the purpose of the forward declaration is to tell the compiler that this type is defined somewhere else. This allows the compiler to generate the correct symbol table.

1:

2://sample.h

3:class foo;

4:class

5:sample

6: {

7:private:

8:foo foo_test (foo &);

9:};

In some large projects, there may be dozens of base classes, which inevitably refer to each other (not satisfying inheritance, but combining relationships). It is necessary to declare each other. Well, it's going to bring some confusion. If the process is not good, it will make a mess, according to my experience, a brief talk about their own approach:

When coding, we generally try to avoid the include header file, but instead use the Declaration class XXX. But sometimes it is necessary to use include header files, so what is the division of the two?

It should be very clear, but it seems to be seldom mentioned in the book.

First of all:
We need to understand why a declaration instead of a header file contains: Yes, to avoid unnecessary recompilation (when a header file is changed). Large projects, low-speed machines, or basic classes often change (unreasonable design bar), the compilation speed will be concerned, in addition, more importantly, the adoption of the Declaration can reduce the coupling between the Code (class), which is a principle of object-oriented design.

II: General principles:
A. include as few as possible in the header file, if you can simply declare class clsold; solve, that's best. reduction of unnecessary include;
B. include as few as possible in the implementation file, not include header files that are not used.

Three: When can you just simply declare class Clsold?
Simply put: it is not necessary to know that the usage of clsold memory layout can be (except for static members), that is, if it is a pointer or a reference way of the line.
Like what:
Clsold * M_pold; Pointer takes 4 bytes long
Clsold & Test (Clsold * pold) {return *pold};
Everything OK.

Four: When can not simply declare class Clsold, must include it?
Not satisfying three of cases:
Like what:
Clsold M_objold; Do not know to occupy the size, it must be calculated by its specific statement
The reason is simple, think you want to calculate sizeof (classnew), but even clsold size don't know, compiler obviously can do nothing.

Special cases:
int Test () {return clsold::m_sint;}
Static member invocation, think that should not need to know the memory layout, but because it is necessary to know that M_sint belongs to the Clsold namespace, if only declaring class XXX is obviously not sufficient to explain, so must include the header file.

To sum up, I have the following suggestions:
1: If there is a common dependency (must include) of the class, such as a and B are dependent on D can be put together, and then directly include "D" class users only care about the type exposed to this class, the internal use of the type does not have to pipe (do not have to go to include D). This gives the class, the caller is better to use (not to see the code lookup, is not also need to include other header files).

2: If Class A relies on D for Class B, you can separate them by two header files. respective include. This avoids the need to re-compile when D is changed.

3: The class should try to invoke other classes with pointers or references, so that only class XXX can be declared. And this is in line with the optimal utilization of resources, more conducive to the use of polymorphism.

C + + header file contains problem-include&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.