Reduce compilation dependencies between files

Source: Internet
Author: User
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

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.