// file old.h
class old {
//公有和保护成员
// public and protected members
private:
//私有成员, 只要任意一个的头文件发生变化或成员个数增加,减少,所有引用old.h的客户端必须重新编译
// private members; whenever these change,
// all client code must be recompiled
};
改写成这样:
// file old.h
class old {
//公有和保护成员
// public and protected members
private:
class oldImpl* pimpl_;
// 替换原来的所有私有成员变量为这个impl指针,指针只需要前向声明就可以编译通过,这种写法将前向声明和定义指针放在了一起, 完全可以。
//当然,也可以分开写
// a pointer to a forward-declared class
};
// file old.cpp
struct oldImpl {
//真正的成员变量隐藏在这里, 随意变化, 客户端的代码都不需要重新编译
// private members; fully hidden, can be
// changed at will without recompiling clients
};
不知道你看明白了没有, 看不明白请随便写个类试验下,我就是这么做的,当然凡事也都有优缺点,下面简单对比下:
|
使用impl 实现类 |
不使用impl实现类 |
|
|
优点 |
类型定义与客户端隔离, 减少#include 的次数,提高编译速度,库端的类随意修改,客户端不需要重新编译 |
直接,简单明了,不需要考虑堆分配,释放,内存泄漏问题 |
|
缺点 |
对于impl的指针必须使用堆分配,堆释放,时间长了会产生内存碎片,最终影响程序运行速度, 每次调用一个成员函数都要经过impl->xxx()的一次转发 |
库端任意头文件发生变化,客户端都必须重新编译 |
改为impl实现后是这样的:
// 只用 file and cx 有虚函数.
#include "file.h"
#include "db.h"
class cx;
class error;
class old : public file, private db {
public:
old( const cx& );
db get_db( int, char* );
cx get_cx( int, cx );
cx& fun1( db );
error fun2( error );
virtual std::ostream& print( std::ostream& ) const;
private:
class oldimpl* pimpl; //此处前向声明和定义
};
inline std::ostream& operator<<( std::ostream& os,const old& old_val )
{ return old_val.print(os); }
//implementation file old.cpp
class oldimpl{
std::list<cx> cx_list_;
deduce dudece_d_;
};










