C++文件依存关系
来源:互联网 发布:人工智能用的什么语言 编辑:程序博客网 时间:2024/05/17 02:09
C++文件依存关系
首先我不给出依存关系的定义,我给出一个例子。
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 };
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 }
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类解除了接口和实现之间的耦合关系,从而降低了文件间的编译依存性。但同时也损耗了一些性能与空间。
- C++文件依存关系
- C++文件依存关系
- 文件间的编译依存关系
- 《Effective C++》读书笔记之item31:将文件间的编译依存关系降至最低
- 《Effective C++》学习笔记条款31:将文件间的编译依存关系降至最低
- Effective C++:条款31:将文件间的编译依存关系将至最低
- 《Effective C++》:条款31:将文件间的编译依存关系降至最低
- 读书笔记《Effective C++》条款31:将文件间的编译依存关系降至最低
- C++之将文件间的编译依存关系降至最低(31)---《Effective C++》
- c++笔记 降低头文件间的编译依存关系
- item31: 将文件间的编译依存关系降至最低
- Effective C++ Item 31 降低文件间编译依存关系
- Effective C++ Item 31 降低文件间编译依存关系
- 将文件间的编译依存关系降至最低
- 将文件间的编译依存关系降至最低
- 自动产生依存关系
- 编译依存关系最小化
- 句法依存关系测试
- java关键字static介绍
- java计算高精度数据
- 使用python的nose模块进行测试
- libcurl使用
- eclipse反编译插件Jadclipse
- C++文件依存关系
- java使用JNI调用C++代码(vs2010生成dll文件)
- Linux 找回密码
- V4L2视频驱动和应用分析
- IE9.0 设置无法保存解决方法 其他版本也可以这么做
- 打印 传参数给HTML页面 同步加载数据
- 类图总结
- String的replaceAll方法替换反斜线报异常
- 彼岸花开,忧伤了谁的等待?诗意伤感日志