In C ++ development, if you do not pay attention to it, modifying the definition in a header file will lead to many references (indlude) to re-compile the file in this header file, which is a waste of time, therefore, we should minimize the compilation dependencies between files.
In addition to the standard library, try not to include the header file of the class to be used in the header file, although it is convenient for client programmers to directly include header files (including your header files, you can directly use the dependent files), it will consume a lot of Compilation Time for large projects and is not desirable.
The difficulty in using a pre-declaration is that the compiler must know the size of its object during compilation. If the class member variable defined in your header file is a custom class, it is best to use pointers to hide class details so that the compiler knows the pointer size.
Class ADDR; // Forward Declaration instead of directly include "ADDR. H"
Class Customer
{
Public:
Customer ();
PRIVATE:
ADDR * myaddr; // Use Pointer instead of directly ADDR
Date * mylastmodified;
};
Replacing the dependency on the class definition with the dependency on the class declaration is the principle of reducing the compilation dependency. You do not need to define a class when declaring a function that uses a class, even if this function is passed or returned by passing values.
Class date; // class declaration
Date today (); // fine-no definition
Void setlastmodified (date D); // fine
Another method is to use a pointer to the implementation, that is, only the interface is defined in your customer definition, and Private Members are encapsulated in a customerimpl, the main class uses a member pointer variable pointing to the implementation class. Although this truly achieves the separation of interfaces and implementations, the customer and the date and the ADDR are separated, but I do not like this, the number of Chinese documents for large projects has increased exponentially.
Using Interfaces and factory is a good method. That is, you only rely on the Interface header file. You do not need to pay attention to the specific implementation class.
For more details, see: http://develop.csai.cn/c/200604060902171527.htm