幸好读到了一个从前没意识到的问题,也算是值了:句柄类,也叫Cheshire Cat。
问题背景是这样的:1)在极为安全的领域,即使核心实现已经封闭在库中不可见,但是头文件中的变量定义仍然可能会曝露一些内部信息; 2)在设计初期,实现部分固然需要经常变动,连头文件中变量定义也需要经常变动,因此在重编译的时候头文件也需要编译,有时候导致编译时间过长。句柄类可以解决这类问题:
//:HANDLE.H -- Handle Classes #ifndef HANDLE_H_ #define HANDLE_H_ class handle { struct cheshire; // Class declaration only cheshire* smile; public: handle( ); void doit( ); ~handle( ); }; #endif // HANDLE_H_
//:HANDLE.CPP -- Handle implementation #include "handle.h" //Define handle‘s implementation struct handle:cheshire { int i; }; handle::handle() { smile=(cheshire*)malloc(sizeof(cheshire)); smile->i=0; } void handle::doit() { //do something with i } handle::~handle() { free(smile); }
句柄类的使用就像任何类的使用一样,包括头文件,创建对象,发送信息。但是通过这样的设计,即隐藏了变量的设计,也使得实现作变动时无需重编译头文件。Bruce说虽然这并不是完美的信息隐蔽,但毕竟是一大进步。
3)项目做大了就会发现,往往一个工程文件代码改一个部分就需要很长时间的编译,那个等待是非常痛苦啊,所以开始在工程的架构上一定要做好充分的准备!
//-------handle.h-------- #ifndef HANDLE_H #define HANDLE_H class Handle { class Test; Test *t; public: void init(); void show(); }; #endif //--------handle.cpp---------- #include "handle.h" class Handle::Test { public: int i; }; void Handle::init() { t=new Test; t->i=0; } void Handle::show() { cout<<t->i<<endl; } //---------main.cpp--------- #include "handle.h" int main() { Handle h; h.init(); h.show(); }
原文:http://blog.csdn.net/dhuwxs/article/details/20769877