In some major projects, dozens of basic classes may be included, which is unable to reference each other (do not satisfy the inheritance relationship, but the combination relationship). That is, it is necessary to declare each other. Ok, this time will bring some confusion. If it is not working, it will make a mess, according to my experience, simply talk about my own treatment:
When encoding, we generally try to avoid include header files, but use the declaration class xxx. But sometimes you must use the include header file, then what is the division of the two? It should be very clear, but the book seems to be less mentioned.
First of all: We must understand why it is necessary to use a statement to replace the header file: Yes, it is to avoid unnecessary recompilation (when the header file is changed). The project is large, the low speed machine, or the basic class often changes (unreasonable design), the compilation speed is still intended, in addition, more importantly, the declaration can reduce the code between the code (Class), this Also a major principle for object-oriented design.
Two: General principles: a. Try to be less include in the header file, if you can declare the class clsold; solve, it is best. Reduce unnecessary include, b. Implementing the files to include less include, do not use the header files that INCLUDE.
Three: When can I just make Class Clsold? Simply put: You don't need to know the usage of Clsold memory layout (except static members), that is, if it is a pointer or reference method. For example: clsold * m_pold; / / pointer up 4 bytes long clsold & test (clsold * Pold) {return * Pold}; everything is OK.
Four: When can I simply declare Class Clsold, I must include INCLUDE? In the case of not satisfying: such as: clsold m_objold; // Don't know that the occupation is very simple, you must calculate the reason very simple. Think of you want to calculate the sizeof (Classnew), but even CLSOLD SIZE does not know The compiler will obviously be powerless. Special circumstances: int Test () {return clsold :: m_sint;} Static member call, you should not need to know the memory layout, but because you need to know that m_sint is a clsold namespace, if only Class XXX is obviously not enough Description, the header file must be included.
In summary, I have the following suggestions: 1: If there is a class of common dependencies (must include), such as A, B relying on D can be put together, then directly include "D" users only need Caring for related types of this category, the types used inside are not used to tubes (do not have to go to include D). The Class of this given is better to use (do not look at the code lookup, is it necessary to include other header files).
2: If the A class does not depend on D, you can separate them two headers. Individual include. This avoids unnecessary recompilation when D changes.
3: Try to call other classes in the category or reference mode, so you can declare the class xxx. And this is also the best utilization of resources, more beneficial to use polymorphism.