如果现在你做的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 }; |