Clause 31 of effective C + +: Minimizing compilation dependencies between files

Source: Internet
Author: User

"Effective C + +"

Article 31: Minimize compilation dependencies between files

Let's say you made some minor changes to a class implementation file for a C + + program. Note that the modification is not the class interface, but rather the implementation, and only the private component is changed. Then rebuild the program and expect it to take just a few seconds. After all, only one class is modified. When you press the "Build" button or type make instructions, you get a shock and then feel embarrassed because you realize that the whole world is being recompiled! So where is the problem???

The problem is that C + + does not "detach the interface from the implementation" to do a good job. The definition of class not only describes the class interface in detail, but also includes a full implementation breakdown. For example:

#include <string> #include "date.h" #include "Address.h" class Person{public:person (const std::string& name,    Const date& Birthday, const address& addr);    std::string name () const;    std::string birthDate () const;    std::string address () const; Private:std::string thename;//Implementation Detail Date thebirthdate;//Implementation Detail Address theaddress;//implementation Breakdown};

which

#include <string> #include "date.h" #include "address.h"

Because of these header files, a compilation dependency is formed between the person definition file and its included files. If any of these header files are changed, or if any of the other header files that these header files depend on change, then each file containing the person class will have to be recompiled, and any files that use the person class must be recompiled. Such a series of compiled dependencies can cause untold disasters for many projects.

Here is the idea of a solution:

Hides the object implementation detail behind a pointer. Divide the person into two classes, one provides only the interface, the other is responsible for implementing the interface. If the so-called implementation class that is responsible for implementation is named Personimpl,person, it will be defined as follows:

#include <string> #include <memory>class Personimpl;//person implementation class of the Date;//person interface with the classes (           date,address) of the predecessor Declaration class Address;class Person{public:person (const std::string& name,const date& Birthday,    Const address& addr);    std::string name () const;    std::string birthDate () const;    std::string address () const; ...private:std::tr1::shared_ptr<personimpl> pimpl;//Smart pointer shared_ptr, pointing to the physical};

The key to this separation is to replace "defined dependencies" with "declared dependencies", which is the nature of the minimization of compilation dependencies: In reality, make the header file as self-fulfilling as possible, and if not, let it be dependent on the declarations in other files. Everything else comes from this simple design strategy:

1. Do not use objects if you are using object reference or object pointers to complete the task.

2. If possible, replace the class definition with the class declaration.

3. Provide different header files for both the declaration and the defined formula.


Handle classes The specific approach 1 is to transfer all their functions to the corresponding implementation class and the latter to do the actual work. For example, the following is the implementation of the person two member function:

#include "Person.h" #include "PersonImpl.h" Person::P Erson (const std::string& name,const date& Birthday,    Const address& Addr): Pimpl (New Personimpl (NAME,BIRTHDAY,ADDR)) {}std::string person::name () const{ return Pimpl->name ();}

Note that the person constructor calls the Personimpl constructor with new, and Personimpl::name is called within the Person::name function. It is important that making a person into a Handle class does not change what it does, it only changes the way it does things.

Another way to make handle class is to make person a special abstract base class called interface class. The purpose of this class is to detail one by one describes the interface of the derived classes, so it usually has no member variables, no constructors, only a virtual destructor and a set of pure virtual functions to describe the entire interface.

Summarize:

    1. The general idea of supporting the minimization of compile dependencies is that it is dependent on the declaration, not on the definition. The two instruments based on this conception are handle classes and interface classes.

    2. The library header file should exist in the form of "full and only stated". This practice applies regardless of whether the design template is used.

2016-11-08 23:10:40

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1870849

Clause 31 of effective C + +: Minimizing compilation dependencies between files

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.