// old.h: 这就是你接收的类
//
#include <iostream>
#include <ostream>
#include <list>
// 5 个 分别是file , db, cx, deduce or error , 水平有限没有模板类
// 只用 file and cx 有虚函数.
#include "file.h" // class file
#include "db.h" // class db
#include "cx.h" // class cx
#include "deduce.h" // class deduce
#include "error.h" // 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:
std::list<cx> cx_list_;
deduce deduce_d_;
};
inline std::ostream& operator<<( std::ostream& os,const old& old_val )
{ return old_val.print(os); }
typedef basic_ostream<char, char_traits<char> > ostream;
inline std::ostream& operator<<( std::ostream& os,const old& old_val )
{ return old_val.print(os); }
// old.h: 这就是你接收的类
//
#include <iosfwd> //新替换的头文件
#include <list>
// 5 个 分别是file , db, cx, deduce or error , 水平有限没有模板类
// 只用 file and cx 有虚函数.
#include "file.h" // class file , 作为基类不能删除,删除了编译器就不知道实例化old 对象时分配多大的空间了
#include "db.h" // class db, 作为基类不能删除,同上
#include "cx.h" // class cx
#include "deduce.h" // class deduce
// error 只被用做参数和返回值类型, 用前向声明替换#include "error.h"
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:
std::list<cx> cx_list_; // cx 是模版类型,既不是函数参数类型也不是函数返回值类型,所以cx.h 头文件不能删除
deduce deduce_d_; // deduce 是类型定义,也不删除他的头文件
};
inline std::ostream& operator<<( std::ostream& os,const old& old_val )
{ return old_val.print(os); }
// 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()的一次转发 | 库端任意头文件发生变化,客户端都必须重新编译 |
// 只用 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_;
};
// 只用 file and cx 有虚函数.
#include "file.h"
class cx;
class error;
class db;
class old : public file {
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_;
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有