C + + minimizes compilation relationships between files

Source: Internet
Author: User

Class definition: class definition, can know the size of the class

Implementation of the class:

Declaration of the class: the declaration of the class, indicating that, with this class, the compilation does not go wrong

C + + does not make "detach interface from implementation" very well. The definition of class not only describes the class interface, but also includes a full implementation prologue. Such as:

Class Person

{

Public

Person (const string& name,const dateg& birthday,const address& addr);

String name () const;

String birthDate () const;

String address () const;

....

Private

String thename;

Date thebirthdate;

Address theaddress;

};

The person here cannot be compiled because the compiler does not see the definition of string,date and address. Such a definition is usually provided by the # include indicator, so there is probably something like this at the top of the person definition file:

#include <string>

#include "date.h"

#include "Address.h"

However, this creates a compilation dependency between the person definition file and its included files. If one of these header files is changed, or if any of the other header files that the 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. This usually causes disaster for large projects.

Workaround:

Consider adding a predecessor declaration to remove the header files included with # include:

Class date;//front-facing declaration
Class address;//front-facing declaration
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://optional, write not write

String thename;

Date thebirthdate;

Address theaddress;


};

With this approach, the coupling can be eliminated, the class person has no dependency on the class date, and the person's client is recompiled only when the person interface is modified.

But there's a problem with this approach:

int main () {    int x;    Person P (params);}
When the compiler sees the definition of x, it knows how much memory must be allocated before it can tolerate the next int. But when the editor sees the definition of P, how do you know how big a person is? The only way to do this is to ask for the definition of class, but if the class definition does not list the implementation details (not knowing the implementation details of class date, etc.)

C + + minimizes compilation relationships between 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.