C++之PIMPL模式
PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。
举例:
//x.h
class X
{
public:
void Fun();
private:
int i; //add int i;
}; //c.h
#include <x.h>
class C
{
public:
void Fun();
private:
X x; //与X的强耦合
}; PIMPL做法:
//c.h
class X; //代替#include <x.h>
class C
{
public:
void Fun();
private:
X *pImpl; //pimpl
};
1)降低模块的耦合。因为隐藏了类的实现,被隐藏的类相当于原类不可见,对隐藏的类进行修改,不需要重新编译原类。
2)降低编译依赖,提高编译速度。指针的大小为(32位)或8(64位),X发生变化,指针大小却不会改变,文件c.h也不需要重编译。
3)接口与实现分离,提高接口的稳定性。
1、通过指针封装,当定义“new C”或"C c1"时 ,编译器生成的代码中不会掺杂X的任何信息。
2、当使用C时,使用的是C的接口(C接口里面操作的类其实是pImpl成员指向的X对象),与X无关,X被通过指针封装彻底的与实现分离。
//c.cpp
C::C()pImpl(new X())
{
} C::~C()
{
delete pImpl;
pImpl = NULL;
} void C::Fun()
{
pImpl->Fun();
} //main
#include <c.h>
int main()
{
C c1;
c1.Fun();
return 0;
}
实例代码:
nestcalss.h
#ifndef __LINE_H__
#define __LINE_H__ //设计模式: PIMPL
//1. 实现信息隐藏
//2. 减小编译依赖, 可以用最小的代价平滑的升级库文件,
//3. 接口与实现进行解耦 class Line
{
public:
Line(int,int,int,int);
~Line(); void printLine() const;
private:
class LineImpl;
private:
LineImpl * _pimpl;
}; #endif
nestclass.cc
#include "nestClass.h"
#include <math.h> #include <iostream>
using std::cout;
using std::endl; class Line::LineImpl
{
class Point
{
public:
Point(int ix = 0, int iy = 0)
: _ix(ix)
, _iy(iy)
{
cout << "Point(int=0, int=0)" << endl;
} void print() const
{
cout << "(" << _ix
<< "," << _iy
<< ")";
} private:
int _ix;
int _iy;
};
public:
LineImpl(int x1, int y1, int x2, int y2)
: _p1(x1, y1)
, _p2(x2, y2)
{
cout << "LineImpl(int,int,int,int)" << endl;
} ~LineImpl() { cout << "~LineImpl()" << endl; } void printLine() const;
private:
Point _p1;
Point _p2;
}; void Line::LineImpl::printLine() const
{
_p1.print();
cout << " --> ";
_p2.print();
cout << endl;
} Line::Line(int x1, int y1, int x2, int y2)
: _pimpl(new LineImpl(x1, y1, x2, y2))
{
cout << "Line(int,int,int,int)" << endl;
} Line::~Line()
{
delete _pimpl;
cout << "~Line()" << endl;
} void Line::printLine() const
{
_pimpl->printLine();
}
main.cc
#include "nestClass.h"
#include <iostream>
using std::cout;
using std::endl; int main(void)
{
Line line(1, 2, 3, 4);
line.printLine(); return 0;
}
C++之PIMPL模式的更多相关文章
- 提高C++编译速度-------pimpl 模式& 桥接模式(转)
pimpl 模式(Private Implementation),我们常常听到诸如“不要改动你的公有接口”这样的建议,所以我们一般都会修改私有接口,但是这会导致包含该头文件的所有源文件都要重新编译,这 ...
- 【C++自我精讲】基础系列六 PIMPL模式
[C++自我精讲]基础系列六 PIMPL模式 0 前言 很实用的一种基础模式. 1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implemen ...
- PIMPL(private implementantion)模式(转载)
前记:请搜索PIMPL(private implementantion)模式和桥接模式, PIMPL是桥接模式的一种典型实现 以下转自:http://blog.csdn.net/nrc_douning ...
- [转]编译防火墙——C++的Pimpl惯用法解析
impl(pointer to implementation, 指向实现的指针)是一种常用的,用来对“类的接口与实现”进行解耦的方法.这个技巧可以避免在头文件中暴露私有细节(见下图1),因此是促进AP ...
- 嵌套类,PIMPL
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 编译防火墙——C++的Pimpl惯用法解析
http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...
- C++ 类的前置声明
http://www.2cto.com/kf/201311/260705.html 今天在研究C++”接口与实现分离“的时候遇到了一个问题,看似很小,然后背后的东西确值得让人深思!感觉在学习的过 ...
- [Effective C++ --029]为“异常安全”而努力是值得的
假设有个class用来表现夹带背景图案的GUI菜单单,这个class用于多线程环境,所以它有个互斥器(mutex)作为并发控制用: class PrettyMenu{ public: ... void ...
- boost之ThreadPool
threadpool是基于boost库实现的一个线程池子库,但线程池实现起来不是很复杂.我们从threadpool中又能学到什么东西呢? 它是基于boost库实现的,如果大家对boost库有兴趣,看看 ...
随机推荐
- C# PropertyGrid控件应用心得 【转】
源文 : http://blog.csdn.net/luyifeiniu/article/details/5426960 c#stringattributesobjectmicrosoftclass ...
- lucene 自定义评分 (给lucene自带的评分*我们filed的系数) 如搜索结果时间的加权
参见孔浩 lucene 22讲 步骤 1.写一个类继承于 CostomScoreQuery -->覆盖getCostomSorceProvider 方法 2.写一个自己的provider(重写c ...
- java.lang.String中的trim()方法的详细说明(转)
String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗? 一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱. 首先我直接反编译String ...
- spoj 1811 LCS - Longest Common Substring (后缀自己主动机)
spoj 1811 LCS - Longest Common Substring 题意: 给出两个串S, T, 求最长公共子串. 限制: |S|, |T| <= 1e5 思路: dp O(n^2 ...
- GOOGLE VR SDK开发VR游戏,VR播放器之中的一个
近期一年来,VR虚拟现实和AR增强现实技术的宣传甚嚣尘上.事实上VR,AR技术非常早就有了,一直没有流行开来.不可否认价格是影响技术推广的最大壁垒. 谷歌对VR最大的贡献是提供了便宜的谷歌眼镜,依照G ...
- swift学习_xcode6搭建
首先是环境搭建 , 我的是苹果系统 , 我是个穷小子. 8k的电脑离我比較遥远. 自己动手的黑苹果 . 总价1k, 学习够用了即可.期间也学到了非常多东西 . 就是穷人仅仅能发时间去换钱了, 一直在考 ...
- 晶振及COMS电路
COMS电路:http://www.docin.com/p-246885507.html 简介:无源晶振和有源晶振 电子线路中的晶体振荡器也分为无源晶振和有源晶振两种类型.无源晶振与有源晶振的英文名称 ...
- NumPy事例练习
因为排版问题直接把jupyter里的截图过来了:暂时就写了这么点小例子,建议在ipython notebook中做测试
- android android:duplicateParentState="true" "false"
今天要做一个效果.组件RelativeLayout上有两个TextView.这两个TextView具有不同的颜色值,如今要的效果是,当RelativeLayout被点击时,整个item有高亮背景. 同 ...
- EasyRTMP实现的rtmp推流的基本协议流程
EasyRTMP介绍 EasyRTMP是结合了多种音视频缓存及网络技术的一个rtmp直播推流端,包括:圆形缓冲区(circular buffer).智能丢帧.自动重连.rtmp协议等等多种技术,能够非 ...