C++文件依存关系

来源:互联网 发布:人工智能用的什么语言 编辑:程序博客网 时间:2024/05/17 02:09

C++文件依存关系

如果现在你做的C++项目(课题)包含的文件没有超过1000个,你可以选择略过此文,不过建议继续浏览。
如果你觉得重新编译文件的时间很短或者时间长一点无所谓,反正需要重新编译,那么你也可以选择略过此文,不过也建议浏览。
如果你想学习或者关心这块内容,那么此文必定会给你带来收获。

首先我不给出依存关系的定义,我给出一个例子。

复制代码
 1 class Peopel{ 2 public: 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     ... 8 private: 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" 4 class Peopel{ 5 public: 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     ...11 private:12     std::string theName;               //名字13     Date theBirthDate;                 //生日14     Image img;                         //图片15 };
复制代码

那么这样People定义文件与该三个文件之间就形成了一种编译依存关系。如果这些头文件任何一个文件被改变,或这些头文件所依赖其他头文件任何改变,那么每一个包含People类的文件就需要重新编译,使用People类文件也需要重新编译。想想如果一个项目包含一个上千的文件,每个文件包含其他几个文件,依次这样下来,改动一个文件内容,那么就需要几乎重新编译整个项目了,这可以说很槽糕了。

我们可以进行如下改动

复制代码
 1 namespace std { 2     class string; 3 } 4 class Date; 5 class Image; 6  7 class Peopel{ 8 public: 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     ...14 private:15     std::string theName;                //名字16     Date theBirthDate;                 //生日17     Image img;                         //图片18 };
复制代码
这样只有People该接口被改变时才会重新编译,但是这样有连个问题,第一点string不是class,它是个typedef basic_string<char> string。因此上述前置声明不正确(附其在stl完全代码);,正确的前置声明比较复杂。其实对于标准库部分,我们仅仅通过#include预处理命令包括进来就可以了。
复制代码
 1 #ifndef __STRING__ 2 #define __STRING__ 3  4 #include <std/bastring.h> 5  6 extern "C++" { 7 typedef basic_string <char> string; 8 // typedef basic_string <wchar_t> wstring; 9 } // extern "C++"10 11 #endif
复制代码
前置声明还有一个问题,就是编译器必须在编译期间知道对象的大小,以便分配空间。
例如:
复制代码
1   int main(int argv,char * argc[ ])2     {3         int 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定义就知道了其分配空间的大小了,一个指针的大小。

复制代码
 1 public PeopleImpl 2 { 3     public: 4         PeopleImple(...) 5         ... 6     private: 7         std::string theName;                //名字 8         Date theBirthDate;                 //生日 9         Image img;                         //图片10
复制代码
这样,People就完全与Date、Imge以及People的实现分离了上面那些类任何修改都不需要重新编译People文件了。另外这样写加强了封装。这样也就降低了文件的依存关系。
这里总结下降低依存性方法:
1.如果可以类声明就不要使用类定义了。
2.将数据通过一个指向该数据的指针表示。
3.为声明式和定义式提供不同的头文件。
  这两个文件必须保持一致性,如果有个声明式被改变了,两个文件都得改变。因此一般会有一个#include一个声明文件而不是前置声明若干函数。
  像People这样定
复制代码
 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 { 9     return pImpl->name( );10 }
复制代码

而另外一种Handle类写法是令People成为一种特殊的abstract base class称为Interface类。看到interface这个关键字或许熟悉C#、java的同学可能已经恍然大悟了。这种接口它不带成员变量,也没有构造函数,只有一个virtual析构函数,以及一组纯虚函数,用来表示整个接口。针对People而写的interface class看起来是这样的。

复制代码
1 class People{2 public:3     virtual ~People( );4     virtual std::string name( ) const = 0;5     virtual Date brithDate( ) const =0;6     virtual Image address( ) const =0;7     ...8 };
复制代码

怎么创建对象呢?它们通常调用一个特殊函数。这样的函数通常称为工厂函数或者虚构造函数。它们返回指针指向动态分配所得对象,而该对象支持interface类的接口。

1   class People {2     public:3         ...4         static People* create(const std::string& name,const Date& brithday, const Image& Img);5     };

支持interface类接口的那个类必须定义出来,而且真正的构造函数必须被调

复制代码
 1 class RealPeople:public People{ 2 public: 3     RealPeople(const std::string& name,const Date& birthday,const Image& Img) 4     :theName(name),theBrithDate(brithday),theImg(Img) 5 {} 6     virtual ~RealPeople() { } 7     std::string name( ) const; 8     Date birthDate( ) const; 9     Image img( ) const;10 private: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 {3     return static_cast<People *>(new RealPerson(name,birthday,Img));4 }

Handle类与interface类解除了接口和实现之间的耦合关系,从而降低了文件间的编译依存性。但同时也损耗了一些性能与空间。

 

 



原创粉丝点击