将文件间的编译依存关系降至最低

#include和类的定义

include “…h”,等于是把Birth和Address类的实现带入了person.h,也就是加入了类的定义

版本一

  1. //person.h
  2. #ifndef PERSON_H
  3. #define PRESON_H
  4. #include "address.h"
  5. #include "birth.h"
  6. #include<iostream>
  7. using namespace std;
  8. class Person {
  9. public:
  10. Person(const Address& a,const Birth& b);
  11. private:
  12. Address p_a;
  13. Birth p_b;
  14. };
  15. #endif
  1. //person.cpp
  2. #include "../include/person.h"
  3. Person::Person(const Address& a,const Birth& b) {
  4. cout << "Person Constructor" << endl;
  5. }
  1. //birth.h
  2. #ifndef BIRTH_H
  3. #define BIRTH_H
  4. #define NUM 1
  5. #include<iostream>
  6. using namespace std;
  7. class Birth {
  8. public:
  9. Birth();
  10. };
  11. #endif
  1. #include "../include/birth.h"
  2. Birth::Birth() {
  3. cout << "Birth constructor:" << NUM << endl;
  4. }
  1. //main.cpp
  2. #include "../include/person.h"
  3. int main() {
  4. Birth b;
  5. Address a;
  6. Person p(a,b);
  7. return 0;
  8. }

1、相关链接
Makefile-只修改了.h头文件,编译为什么不起作用?
Linux Makefile 生成 *.d 依赖文件以及 gcc -M -MF -MP 等相关选项说明
Makefile include文件包含
Makefile中的include命令详解
#ifdef与#endif的作用及用法
.h.gch文件是什么?
2、第一次编译
image.png
image.png
修改NUM之后,第二次编译
image.png
生成了.d文件
image.png
3、结论
Person所包含的任何一个头文件被修改,每一个包含Person class包括Person.cpp文件都要被重新编译。

版本二 尝试将接口和实现分离

1、为了实现接口和实现分离,把#include “…h”删掉,当然Person相关不会重新编译了,但是也不能通过编译了
2、为了通过编译,添加了类的前置声明,将接口和实现分离

  1. //person.h
  2. #ifndef PERSON_H
  3. #define PRESON_H
  4. #include<iostream>
  5. using namespace std;
  6. class Address;
  7. class Birth;
  8. class Person {
  9. public:
  10. Person(const Address& a,const Birth& b);
  11. };
  12. #endif

也是不能通过编译的,因为main.cpp使用了Person的定义式,需要知道Person的大小
image.png

版本三 pImpl制作Handle Class

pImpl

pImpl
PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。
pImpl指针指向Person类的实现 - 即Birth的实现和Address的实现,等同于Birth和Address

  1. //person.h
  2. #ifndef PERSON_H
  3. #define PRESON_H
  4. #include<iostream>
  5. #include<memory>
  6. using namespace std;
  7. class PersonImpl;
  8. class Address;
  9. class Birth;
  10. class Person {
  11. public:
  12. Person(const Address& a,const Birth& b);
  13. private:
  14. Address* a;
  15. Birth* b;
  16. };
  17. #endif
  1. //main.cpp
  2. #include "../include/person.h"
  3. int main() {
  4. /*Birth b;
  5. Address a;
  6. Person p(a,b);*/
  7. Person* p;
  8. return 0;
  9. }

什么时候不需要类的定义

1、如果声明为一个指针呢?指针大小是固定的,声明是够用了,不会报错,但是没有调用类的构造函数。
image.png
2、什么时候不需要class的定义式
1)引用 和 指针
2)只是声明一个函数时,如果是作为函数实参和函数返回值,class声明就足够了,不需要class的定义(#include)

什么时候需要类的定义

1、对象的声明近似于定义,所以一旦出现了

  1. Person p;

就要用到该类型的定义式。
2、一旦要调用这个函数,就需要class的定义(#include)了

cpp发生变化而h没有变

image.png
birth.cpp发生变化但是birth.h没有变,只会重新编译birth
原因是birth的接口没有变化,只是实现细节发生了变化,所以Person和main不会重新编译

h发生变化而cpp没有变

1、只有birth.cpp包含birth.h
image.png
birth.cpp重新编译
2、birth.cpp和main.cpp包含birth.h
image.png
birth.cpp和main.cpp重新编译

结论

如果头文件变了,那么包含这个头文件的实现都要重新编译
但如果它的实现文件(cpp文件)变了,而头文件没有变(对外的接口不变),那么只有该实现文件重新编译。

Handle Class

避免大量依赖性编译的解决方案就是:在头文件中用class声明外来类,用指针或引用代替变量的声明;在cpp文件中包含外来类的头文件。头文件里只需要关心声明,调用函数交给cpp。
比如头文件的类里声明了一个函数

  1. class Date;
  2. class Person {
  3. ……
  4. void getDate(Date d);
  5. };

某个客户不需要getDate函数,就可以不在客户的cpp文件里#include “date.h”,如果没那么需要的头文件修改了,客户的cpp也不会重新编译,从而降低“没那么需要的头文件”和客户cpp的编译依存性。

版本四 版本三的优化

使用智能指针
pImpl的原始指针
C++构造函数初始化列表中不能使用this指针

  1. //personImpl.h
  2. class Birth;
  3. class Address;
  4. class personImpl {
  5. public:
  6. Address* a;
  7. Birth* b;
  8. };
  1. //person.h
  2. class Person {
  3. ……
  4. shared_ptr<PersonImpl> pImpl;
  5. };

版本五 Interface class

“undefined reference to” 问题解决方法
1、避免重编的诀窍就是保持头文件(接口)不变化,而保持接口不变化的诀窍就是不在里面声明编译器需要知道大小的变量,Handler Classes的处理就是把变量换成变量的地址(指针),头文件只有class xxx的声明,而在cpp里面才包含xxx的头文件。Interface Classes则是利用继承关系和多态的特性,在父类里面只包含成员方法(成员函数),而没有成员变量。

  1. //person.h
  2. #ifndef PERSON_H
  3. #define PERSON_H
  4. //pure virtual class
  5. #include<memory>
  6. class Birth;
  7. class Person {
  8. public:
  9. virtual ~Person() {}
  10. virtual void printBirth() const = 0;
  11. //create Person object
  12. static std::shared_ptr<Person> factoryFunc(const Birth& b);
  13. };
  14. #endif
  1. //realPerson.h
  2. #ifndef REALPERSON_H
  3. #define REALPERSON_H
  4. #include "../include/person.h"
  5. #include "../include/birth.h"
  6. #include<iostream>
  7. class RealPerson : public Person {
  8. public:
  9. RealPerson(const Birth& b);
  10. virtual ~RealPerson();
  11. void printBirth() const;
  12. private:
  13. Birth b;
  14. };
  15. #endif

在RealPerson.cpp里面去实现方法。从这里我们可以看到,只有子类里面才有成员变量,也就是说,如果Birth的头文件变化了,那么子类一定会重编,所有用到子类头文件的文件也要重编,所以为了防止重编,应该尽量少用子类的对象。利用多态特性,我们可以使用父类的指针,像这样Person* p = new RealPerson(xxx),然后p->printBirth()实际上是调用了子类的printBirth()方法。

  1. //main.cpp
  2. #include "../include/person.h"
  3. #include "../include/birth.h"
  4. int main() {
  5. std::string str = "birth";
  6. Birth b(str);
  7. //create Person pointer
  8. std::shared_ptr<Person> pp(Person::factoryFunc(b));
  9. pp->printBirth();
  10. return 0;
  11. }

但这样还有一个问题,就是new RealPerson()这句话一写,就需要RealPerson的构造函数,那么RealPerson的头文件就要暴露了,这样可不行。还是只能用Person的方法,所以我们在Person.h里面加上这个方法:

  1. //create Person object
  2. static std::shared_ptr<Person> factoryFunc(const Birth& b);

注意这个方法是静态的(没有虚特性),它被父类和所有子类共有,可以在子类中去实现它
static函数返回子类对象

  1. std::shared_ptr<Person> Person::factoryFunc(const Birth& b) {
  2. return std::shared_ptr<Person>(new RealPerson(b));
  3. }

就可以减少编译依赖了。
2、一句话就是:父类是抽象类,没有任何成员变量,所以也不需要任何#include头文件,子类负责实现父类,因为子类里有成员变量,所以为了减少编译相关,在客户端使用的时候尽量不要使用子类的对象。
但是因为多态Person* p = new RealPerson(),为了解决这个问题,可以在父类里设计一个static函数创建Person对象,在子类实现这个static函数。
然后客户端直接调用这个static函数从而实现 创建Person对象的操作。
3、Handler classes将所有外来类对象都放在了pImpl指针里,通过pImpl指针访问Person类的成员函数,pImpl里的原始指针必须初始化(动态分配内存)
Interface classes利用多态特性,在客户端只需要使用到Person的引用或者指针,就可以访问到子类的方法。、