In C ++, whether the header file contains the pre-declaration of include or include classes

Source: Internet
Author: User

From: http://blog.csdn.net/suwei19870312/article/details/6665305

In some large projects, there may be dozens of basic classes, which are inevitably referenced by each other (not satisfying the inheritance relationship, but composite relationship ). That is, mutual declarations are required. Well, this will bring some confusion. If the problem is not handled well, it will make a mess. Based on my experience, I will briefly talk about my own solutions:

When encoding, we usually try to avoid include header files, but use Declaration class XXX. But sometimes the include header file must be used. What is the division of the two?

It should be very clear, but it seems that it is rarely mentioned in the book.

First:

We need to understand why we should replace header file inclusion with declaration: Right, to avoid unnecessary re-compilation (when the header file is changed ). Large projects, low-speed machines, or frequently changing basic classes (unreasonable design), the Compilation speed will still be concerned, and more importantly, the use of declarations can reduce the code (class) this is also a major principle of object-oriented design.

II. General principles:

A. Try to include as few files as possible in the header file. If you can simply declare class clsold; solve the problem, it is best. Reduce Unnecessary include;

B. Try to include as few files as possible. Do not include unused header files.

Iii. When can we simply declare class clsold?

Simply put, you do not need to know how to use the clsold memory layout (except for static members), that is, if it is a pointer or reference method.

For example:

Clsold * m_pold; // pointer occupies 4 bytes long

Clsold & Test (clsold * pold) {return * pold };

Everything is OK.

4. When can I simply declare class clsold and must include it?

If three conditions are not met:

For example:

Clsold m_objold; // The occupied size must be calculated based on its statement.

The reason is simple. If you want to calculate sizeof (classnew), but you don't even know the size of clsold, the compiler is obviously powerless.

Special cases:

Int test () {return clsold: m_sint ;}

For static member calls, you do not need to know the memory layout, but you need to know that m_sint belongs to the clsold namespace. If you declare only class XXX, it is obviously not enough to explain, therefore, the header file must be included.

To sum up, I have the following suggestions:

1: if there are classes with common dependencies (must include), such as a and B Dependent on D, they can be put together, then, users of the include "D" class only need to care about the types exposed by this class, And the types used internally do not need to be managed (they do not need to include D ). The class provided in this way is better for the caller (you don't need to look up the code to find out if you still need to include other header files ).

2: If Class A depends on Class D and Class B does not rely on Class D, you can separate them into two header files. Include. This avoids unnecessary re-Compilation When d changes.

3: Use pointers or references to call other classes as much as possible, so that only class xxx can be declared. In addition, this is in line with the optimal utilization of resources and is more conducive to the use of polymorphism.

/--------------------------------------------------////////////////////////////////////// ///////////////////////////////////////

It is worth noting that the header file in VC ++ contains

Today, when I used VC ++ to compile the code I wrote yesterday, I found that the code is always passable. Most of the errors are related to the definition of a class.

Clearly, I have defined a member variable in this class, but the compiler must say that the variable is not a member of this class. No way. I still have no clue why I found it for half a day. Again, confirm that it is not my c ++ syntax error. I began to doubt whether it was a header file problem. Because when I move some # include "..." To # pragma once or later, the error report will change. Sometimes I just say that one of my classes has repeated definitions.

In the past, when I used C to write header files, I used macro definitions to avoid repeated header files. This method is also available in C ++, but Vc ++ does not need it. It uses # pragma once. This change is a bit confusing for me. I don't know # How Pragma once works. The problem I encountered is related to this issue. Find the content on the Internet. After searching for Google for a long time, the answers in those forums are basically "preventing repeated header files from being included. You don't need to worry about it ." This is the case. But is it true? I have just learned VC ++ and I have not read this content in my book. Why can't I find it online? After my unremitting efforts, I finally found an article at http://www.yesky.com/#/1736627_3.shtml.

The author's adding statement is of the greatest help to me: "Why should I add" class cviewerview; "and other code before class cmainframe? If you replace it with an included file, will it work? Many visual c ++ books avoid these problems, but this is actually an important issue. If you cannot understand the above Code, it is likely that it will be difficult to compile the code. These problems occur based on the fact that when we design a program using standard C/C ++, there is a principle that two code files cannot contain each other, in addition, multiple inclusion may also cause repeated definition errors. To solve this problem, use # pragma in Visual C ++
Once to notify the compiler to include (open) only once during production. That is to say, after # include for the first time, the compiler will no longer include (open) or read these contained files when it is re-generated, therefore, we can see that the # pragma once statement in the header files of all classes created using the Wizard is not surprising. However, it is because of this statement that the compiler cannot correctly identify the referenced class after the second # include operation. Therefore, we also need to add statements such as class cviewerview to notify the compiler that this class is an actual call. "

This is the problem. After I added the declaration of those classes to my code, the compilation was successful. I think this problem should be worth noting in VC ++. Maybe it's because I just learned VC ++, so I have little knowledge. No matter how you write it down, someone may be worrying about this # pragma once.

Header file inclusion is actually a very cumbersome task. We are not only tired, but also very tired when compiling the compiler, coupled with the macro definition that often appears in the header file. It seems that the expansion of various macro definitions is very time-consuming and far slower than the expansion of custom functions. I only propose two principles for the structure of the sentence between different header files and source files, for your reference only:

The first principle should be: do not include header files if they are not included. At this time, the pre-declaration can solve the problem. If you only use a class pointer and do not use a specific object (non-pointer) of the class or access a specific member of the class, you can use the pre-declaration. Because the size of the pointer data type is specific, the compiler can understand it.

The second principle should be to include the header file in the CPP file as much as possible, rather than in the header file. Assume that a member of Class A is a pointer to Class B. in the header file of Class A, the pre-declaration of Class B is used and the result is cheap, so in the implementation of a, we need to access the specific members of B, so we need to include the header file, then we should in the implementation part of Class A (CPP file) contains Class B header files rather than declarative parts (H files ).

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.