首先我不给出依存关系的定义,我给出一个例子。
1class Peopel{ 2public: 3 People(const std::string & name,const Date& brithday,Image Img) 4 std::string name( ) const; 5 Date birthDate( ) const; 6 Image img( ) const; 7 ... 8private: 9 std::string theName; //名字10 Date theBirthDate; //生日11 Image img; //图片12 };
如果编译器没有知道类string,Date和Image的定义,class People是无法通过编译的。一般该定义式是由#include包含的头文件所提供的,所以一般People上面有这些预处理命令
1 #include <string> 2 #include "date.h" 3 #inblude "image.h" 4class Peopel{ 5public: 6 People(const std::string & name,const Date& brithday,Image Img) 7 std::string name( ) const; 8 Date birthDate( ) const; 9 Image img( ) const;10 ...11private:12 std::string theName; //名字13 Date theBirthDate; //生日14 Image img; //图片15 };
那么这样People定义文件与该三个文件之间就形成了一种编译依存关系。如果这些头文件任何一个文件被改变,或这些头文件所依赖其他头文件任何改变,那么每一个包含People类的文件就需要重新编译,使用People类文件也需要重新编译。想想如果一个项目包含一个上千的文件,每个文件包含其他几个文件,依次这样下来,改动一个文件内容,那么就需要几乎重新编译整个项目了,这可以说很槽糕了。
我们可以进行如下改动
1namespace std { 2classstring; 3} 4class Date; 5class Image; 6 7class Peopel{ 8public: 9 People(const std::string & name,const Date& brithday,Image& Img)10 std::string name( ) const;11 Date birthDate( ) const;12 Image img( ) const;13 ...14private:15 std::string theName; //名字16 Date theBirthDate; //生日17 Image img; //图片18 };
1#ifndef __STRING__ 2#define __STRING__ 3 4 #include <std/bastring.h> 5 6extern"C++(www.cppentry.com)" { 7 typedef basic_string <char> string; 8// typedef basic_string <wchar_t> wstring; 9 } // extern "C++(www.cppentry.com)"1011#endif
1int main(int argv,char * argc[ ])2 {3int x;4 People p( 参数 );5 ...6 }
当编译器看到x的定义式,它知道必须分配多少内存,但是看到p定义式就无法知道了。但是如果设置为指针的话,就清楚了,因为指针本身大小编译器是知道的。
#include <string>#include <memory>class PeopleImpl;class Date;class Image;class People{public: People(const std::string & name, const Date& brithday, const Image &Img); std::string name( ) const; Date birthDate( ) const; Imge img( ) const; ...private: PeopleImpl * pImpl;}
PeopleImpl包含下面这三个数据,而People的成员变量指针指向这个PeopleImpl,那么现在编译器通过People定义就知道了其分配空间的大小了,一个指针的大小。
1public PeopleImpl 2{ 3public: 4 PeopleImple(...) 5 ... 6private: 7 std::string theName; //名字 8 Date theBirthDate; //生日 9 Image img; //图片10 }
1 #include "People.h" 2 #include "PeopleImpl.h" 3 4 People::People(const std::string& name, const Date& brithday, const Image& Img) 5 :pImpl(new PersonImpl(name,brithday,addr)) 6{ } 7 std::string People::name( ) const 8{ 9return pImpl->name( );10 }
而另外一种Handle类写法是令People成为一种特殊的abstract base class称为Interface类。看到interface这个关键字或许熟悉C#、java的同学可能已经恍然大悟了。这种接口它不带成员变量,也没有构造函数,只有一个virtual析构函数,以及一组纯虚函数,用来表示整个接口。针对People而写的interface class看起来是这样的。
1class People{2public:3virtual ~People( );4virtual std::string name( ) const = 0;5virtual Date brithDate( ) const =0;6virtual Image address( ) const =0;7 ...8 };
怎么创建对象呢?它们通常调用一个特殊函数。这样的函数通常称为工厂函数或者虚构造函数。它们返回指针指向动态分配所得对象,而该对象支持interface类的接口。
1class People {2public:3 ...4static People* create(const std::string& name,const Date& brithday, const Image& Img);5 };
支持interface类接口的那个类必须定义出来,而且真正的构造函数必须被调用
1class RealPeople:public People{ 2public: 3 RealPeople(const std::string& name,const Date& birthday,const Image& Img) 4 :theName(name),theBrithDate(brithday),theImg(Img) 5{} 6virtual ~RealPeople() { } 7 std::string name( ) const; 8 Date birthDate( ) const; 9 Image img( ) const;10private:11 std::string theName;12 Date theBirthDate;13 Image theImg;14 }
有了RealPeople类,我们People::create可以这样写
1 People* People::create(const std::string& name, const Date& birthday, const Image& Img)2{3return static_cast<People *>(new RealPerson(name,birthday,Img));4 }
Handle类与interface类解除了接口和实现之间的耦合关系,从而降低了文件间的编译依存性。但同时也损耗了一些性能与空间。