#include和类的定义
include “…h”,等于是把Birth和Address类的实现带入了person.h,也就是加入了类的定义
版本一
//person.h#ifndef PERSON_H#define PRESON_H#include "address.h"#include "birth.h"#include<iostream>using namespace std;class Person {public:Person(const Address& a,const Birth& b);private:Address p_a;Birth p_b;};#endif
//person.cpp#include "../include/person.h"Person::Person(const Address& a,const Birth& b) {cout << "Person Constructor" << endl;}
//birth.h#ifndef BIRTH_H#define BIRTH_H#define NUM 1#include<iostream>using namespace std;class Birth {public:Birth();};#endif
#include "../include/birth.h"Birth::Birth() {cout << "Birth constructor:" << NUM << endl;}
//main.cpp#include "../include/person.h"int main() {Birth b;Address a;Person p(a,b);return 0;}
1、相关链接
Makefile-只修改了.h头文件,编译为什么不起作用?
Linux Makefile 生成 *.d 依赖文件以及 gcc -M -MF -MP 等相关选项说明
Makefile include文件包含
Makefile中的include命令详解
#ifdef与#endif的作用及用法
.h.gch文件是什么?
2、第一次编译

修改NUM之后,第二次编译
生成了.d文件
3、结论
Person所包含的任何一个头文件被修改,每一个包含Person class包括Person.cpp文件都要被重新编译。
版本二 尝试将接口和实现分离
1、为了实现接口和实现分离,把#include “…h”删掉,当然Person相关不会重新编译了,但是也不能通过编译了
2、为了通过编译,添加了类的前置声明,将接口和实现分离
//person.h#ifndef PERSON_H#define PRESON_H#include<iostream>using namespace std;class Address;class Birth;class Person {public:Person(const Address& a,const Birth& b);};#endif
也是不能通过编译的,因为main.cpp使用了Person的定义式,需要知道Person的大小
版本三 pImpl制作Handle Class
pImpl
pImpl
PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。
pImpl指针指向Person类的实现 - 即Birth的实现和Address的实现,等同于Birth和Address
//person.h#ifndef PERSON_H#define PRESON_H#include<iostream>#include<memory>using namespace std;class PersonImpl;class Address;class Birth;class Person {public:Person(const Address& a,const Birth& b);private:Address* a;Birth* b;};#endif
//main.cpp#include "../include/person.h"int main() {/*Birth b;Address a;Person p(a,b);*/Person* p;return 0;}
什么时候不需要类的定义
1、如果声明为一个指针呢?指针大小是固定的,声明是够用了,不会报错,但是没有调用类的构造函数。
2、什么时候不需要class的定义式
1)引用 和 指针
2)只是声明一个函数时,如果是作为函数实参和函数返回值,class声明就足够了,不需要class的定义(#include)
什么时候需要类的定义
1、对象的声明近似于定义,所以一旦出现了
Person p;
就要用到该类型的定义式。
2、一旦要调用这个函数,就需要class的定义(#include)了
cpp发生变化而h没有变

birth.cpp发生变化但是birth.h没有变,只会重新编译birth
原因是birth的接口没有变化,只是实现细节发生了变化,所以Person和main不会重新编译
h发生变化而cpp没有变
1、只有birth.cpp包含birth.h
birth.cpp重新编译
2、birth.cpp和main.cpp包含birth.h
birth.cpp和main.cpp重新编译
结论
如果头文件变了,那么包含这个头文件的实现都要重新编译
但如果它的实现文件(cpp文件)变了,而头文件没有变(对外的接口不变),那么只有该实现文件重新编译。
Handle Class
避免大量依赖性编译的解决方案就是:在头文件中用class声明外来类,用指针或引用代替变量的声明;在cpp文件中包含外来类的头文件。头文件里只需要关心声明,调用函数交给cpp。
比如头文件的类里声明了一个函数
class Date;class Person {……void getDate(Date d);};
某个客户不需要getDate函数,就可以不在客户的cpp文件里#include “date.h”,如果没那么需要的头文件修改了,客户的cpp也不会重新编译,从而降低“没那么需要的头文件”和客户cpp的编译依存性。
版本四 版本三的优化
使用智能指针
pImpl的原始指针
C++构造函数初始化列表中不能使用this指针
//personImpl.hclass Birth;class Address;class personImpl {public:Address* a;Birth* b;};
//person.hclass Person {……shared_ptr<PersonImpl> pImpl;};
版本五 Interface class
“undefined reference to” 问题解决方法
1、避免重编的诀窍就是保持头文件(接口)不变化,而保持接口不变化的诀窍就是不在里面声明编译器需要知道大小的变量,Handler Classes的处理就是把变量换成变量的地址(指针),头文件只有class xxx的声明,而在cpp里面才包含xxx的头文件。Interface Classes则是利用继承关系和多态的特性,在父类里面只包含成员方法(成员函数),而没有成员变量。
//person.h#ifndef PERSON_H#define PERSON_H//pure virtual class#include<memory>class Birth;class Person {public:virtual ~Person() {}virtual void printBirth() const = 0;//create Person objectstatic std::shared_ptr<Person> factoryFunc(const Birth& b);};#endif
//realPerson.h#ifndef REALPERSON_H#define REALPERSON_H#include "../include/person.h"#include "../include/birth.h"#include<iostream>class RealPerson : public Person {public:RealPerson(const Birth& b);virtual ~RealPerson();void printBirth() const;private:Birth b;};#endif
在RealPerson.cpp里面去实现方法。从这里我们可以看到,只有子类里面才有成员变量,也就是说,如果Birth的头文件变化了,那么子类一定会重编,所有用到子类头文件的文件也要重编,所以为了防止重编,应该尽量少用子类的对象。利用多态特性,我们可以使用父类的指针,像这样Person* p = new RealPerson(xxx),然后p->printBirth()实际上是调用了子类的printBirth()方法。
//main.cpp#include "../include/person.h"#include "../include/birth.h"int main() {std::string str = "birth";Birth b(str);//create Person pointerstd::shared_ptr<Person> pp(Person::factoryFunc(b));pp->printBirth();return 0;}
但这样还有一个问题,就是new RealPerson()这句话一写,就需要RealPerson的构造函数,那么RealPerson的头文件就要暴露了,这样可不行。还是只能用Person的方法,所以我们在Person.h里面加上这个方法:
//create Person objectstatic std::shared_ptr<Person> factoryFunc(const Birth& b);
注意这个方法是静态的(没有虚特性),它被父类和所有子类共有,可以在子类中去实现它
static函数返回子类对象
std::shared_ptr<Person> Person::factoryFunc(const Birth& b) {return std::shared_ptr<Person>(new RealPerson(b));}
就可以减少编译依赖了。
2、一句话就是:父类是抽象类,没有任何成员变量,所以也不需要任何#include头文件,子类负责实现父类,因为子类里有成员变量,所以为了减少编译相关,在客户端使用的时候尽量不要使用子类的对象。
但是因为多态Person* p = new RealPerson(),为了解决这个问题,可以在父类里设计一个static函数创建Person对象,在子类实现这个static函数。
然后客户端直接调用这个static函数从而实现 创建Person对象的操作。
3、Handler classes将所有外来类对象都放在了pImpl指针里,通过pImpl指针访问Person类的成员函数,pImpl里的原始指针必须初始化(动态分配内存)
Interface classes利用多态特性,在客户端只需要使用到Person的引用或者指针,就可以访问到子类的方法。、
