首页 > 编程 > C++ > 正文

C++文件的依赖关系

2020-02-24 14:35:28
字体:
来源:转载
供稿:网友

如果你正要学习或者对于C++比较上心的朋友可以看看此文,或许能给你意想不到的收获喔,那闲话不多说了,一起来了解下C++文件的依赖关系。

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


 class Peopel{
 public:
     People(const std::string & name,const Date& brithday,Image Img)
     std::string name( ) const;
     Date birthDate( ) const;
     Image img( ) const;
     ...
 private:
     std::string theName;               //名字
     Date theBirthDate;                 //生日
     Image img;                         //图片
 };


如果编译器没有知道类string,Date和Image的定义,class People是无法通过编译的。一般该定义式是由#include包含的头文件所提供的,所以一般People上面有这些预处理命令

 

 


  #include
  #include "date.h"
  #inblude "image.h"
 class Peopel{
 public:
     People(const std::string & name,const Date& brithday,Image Img)
     std::string name( ) const;
     Date birthDate( ) const;
     Image img( ) const;
     ...
 private:
     std::string theName;               //名字
     Date theBirthDate;                 //生日
     Image img;                         //图片
 };


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

 

我们可以进行如下改动


 namespace std {
     class string;
 }
 class Date;
 class Image;

 class Peopel{
 public:
     People(const std::string & name,const Date& brithday,Image& Img)
    std::string name( ) const;
    Date birthDate( ) const;
    Image img( ) const;
    ...
private:
    std::string theName;                //名字
    Date theBirthDate;                 //生日
    Image img;                         //图片
};


这样只有People该接口被改变时才会重新编译,但是这样有连个问题,第一点string不是class,它是个typedef basic_string string。因此上述前置声明不正确(附其在stl完全代码);,正确的前置声明比较复杂。其实对于标准库部分,我们仅仅通过#include预处理命令包括进来就可以了。

 

 


 #ifndef __STRING__
 #define __STRING__

 #include

 extern "C++" {
 typedef basic_string string;
 // typedef basic_string wstring;
 } // extern "C++"

 

#endif


前置声明还有一个问题,就是编译器必须在编译期间知道对象的大小,以便分配空间。
例如:

 

 


  int main(int argv,char * argc[ ])
    {
        int x;
        People p( 参数 );
        ...
    }


当编译器看到x的定义式,它知道必须分配多少内存,但是看到p定义式就无法知道了。但是如果设置为指针的话,就清楚了,因为指针本身大小编译器是知道的。

 

 


#include
#include

 

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定义就知道了其分配空间的大小了,一个指针的大小。

 

 


 public PeopleImpl
 {
     public:
         PeopleImple(...)
         ...
     private:
         std::string theName;                //名字
         Date theBirthDate;                 //生日
         Image img;                         //图片


这样,People就完全与Date、Imge以及People的实现分离了上面那些类任何修改都不需要重新编译People文件了。另外这样写加强了封装。这样也就降低了文件的依存关系。
这里总结下降低依存性方法:

 

 

 

1.如果可以类声明就不要使用类定义了。
2.将数据通过一个指向该数据的指针表示。
3.为声明式和定义式提供不同的头文件。
  这两个文件必须保持一致性,如果有个声明式被改变了,两个文件都得改变。因此一般会有一个#include一个声明文件而不是前置声明若干函数。
  像People这样定 
 


 #include "People.h"
 #include "PeopleImpl.h"

 People::People(const std::string& name, const Date& brithday, const Image& Img)
 :pImpl(new PersonImpl(name,brithday,addr))
 { }
 std::string People::name( ) const
 {
     return pImpl->name( );
 }


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

 

 


 class People{
 public:
     virtual ~People( );
     virtual std::string name( ) const = 0;
     virtual Date brithDate( ) const =0;
     virtual Image address( ) const =0;
     ...
 };


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

 

 


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


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

 

 


 class RealPeople:public People{
 public:
     RealPeople(const std::string& name,const Date& birthday,const Image& Img)
     :theName(name),theBrithDate(brithday),theImg(Img)
 {}
     virtual ~RealPeople() { }
     std::string name( ) const;
     Date birthDate( ) const;
     Image img( ) const;
 private:
     std::string theName;
     Date theBirthDate;
     Image theImg;
 }


有了RealPeople类,我们People::create可以这样写

 

 


 People* People::create(const std::string& name, const Date& birthday, const Image& Img)
 {
     return static_cast(new RealPerson(name,birthday,Img));
 }


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

以上就是C++文件的依赖关系的介绍,如果大家想了解更多相关内容,请持续关注本站,本站小编将在第一时间为大家带来更好的经典内容。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表