1 PIMPL解释

PIMPL(Private Implementation 或 Pointer to Implementation)是通过一个私有的成员指针,将指针所指向的类的内部实现数据进行隐藏。

2 PIMPL优点

举例:

//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模式的更多相关文章

  1. 提高C++编译速度-------pimpl 模式& 桥接模式(转)

    pimpl 模式(Private Implementation),我们常常听到诸如“不要改动你的公有接口”这样的建议,所以我们一般都会修改私有接口,但是这会导致包含该头文件的所有源文件都要重新编译,这 ...

  2. 【C++自我精讲】基础系列六 PIMPL模式

    [C++自我精讲]基础系列六 PIMPL模式 0 前言 很实用的一种基础模式. 1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implemen ...

  3. PIMPL(private implementantion)模式(转载)

    前记:请搜索PIMPL(private implementantion)模式和桥接模式, PIMPL是桥接模式的一种典型实现 以下转自:http://blog.csdn.net/nrc_douning ...

  4. [转]编译防火墙——C++的Pimpl惯用法解析

    impl(pointer to implementation, 指向实现的指针)是一种常用的,用来对“类的接口与实现”进行解耦的方法.这个技巧可以避免在头文件中暴露私有细节(见下图1),因此是促进AP ...

  5. 嵌套类,PIMPL

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  6. 编译防火墙——C++的Pimpl惯用法解析

    http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...

  7. C++ 类的前置声明

    http://www.2cto.com/kf/201311/260705.html    今天在研究C++”接口与实现分离“的时候遇到了一个问题,看似很小,然后背后的东西确值得让人深思!感觉在学习的过 ...

  8. [Effective C++ --029]为“异常安全”而努力是值得的

    假设有个class用来表现夹带背景图案的GUI菜单单,这个class用于多线程环境,所以它有个互斥器(mutex)作为并发控制用: class PrettyMenu{ public: ... void ...

  9. boost之ThreadPool

    threadpool是基于boost库实现的一个线程池子库,但线程池实现起来不是很复杂.我们从threadpool中又能学到什么东西呢? 它是基于boost库实现的,如果大家对boost库有兴趣,看看 ...

随机推荐

  1. 如何将MID音乐转换成MP3

    1 使用Direct MIDI to MP3 Converter这个软件,你可以从下面这个网站下载:http://www.hanzify.org/index.php?Go=Show::List& ...

  2. python(10)- 字符编码

    一 什么是编码? 基本概念很简单.首先,我们从一段信息即消息说起,消息以人类可以理解.易懂的表示存在.我打算将这种表示称为“明文”(plain text).对于说英语的人,纸张上打印的或屏幕上显示的英 ...

  3. python(33)- 模块与包

    一 模块 1 什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2 为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函 ...

  4. 2014年8月25日,收藏家和杀手——面向对象的C++和C(一)

    近期事情特别多,睡眠也都非常晚,有点精神和身体混乱的感觉,所以想写写技术分析文章.让两者的我都调整一下.这篇技术分析文章是一直想写的,当前仅仅是开篇,有感觉的时候就写写,属于拼凑而成,兴许的篇章没有时 ...

  5. centos 6 7 differences 区别

    命令 centos6 centos7 ifconfig 有 有 yum install -y net-tools 服务管理 chkconfig /etc/init.d/服务 systemctl sys ...

  6. 【BZOJ2625】[Neerc2009]Inspection 最小流

    [BZOJ2625][Neerc2009]Inspection Description You are in charge of a team that inspects a new ski reso ...

  7. ffmpeg ffplay播放延时大问题:播放延时参数设置

    使用ffplay播放视频源时,rtsp/rtmp等,会有一定的延时,这里我们可以通过设置ffplay播放参数将延时控制到最小. ffplay.exe -i rtmp://xxxxxxx -fflags ...

  8. Delphi里可将纯虚类实例化,还可调用非虚函数

    这是与Java/C++的巨大不同.目前还没仔细想这个特征与TClass之间的联系,先记住结论再说.以后再回来修改这个帖子. unit Unit1; interface uses Windows, Me ...

  9. zsh 的简单介绍

    什么是 zsh,要想解释好这个问题,那么得先说明什么是 shell.不负责任的解释说法就是 shell 就是一个壳.这个壳可不是蜗牛的壳,而是计算机的一个壳,当然也不是计算机的外壳啦,这个壳是相对于计 ...

  10. Mall电商项目总结(一)——项目概述

    项目概述 此电商项目为本人学习项目,后端 使用nginx实现负载均衡转发请求到多台tomcat服务器,使用多台 redis服务器分布式 缓存用户登录信息. 项目已经部署到阿里云服务器,从阿里云linu ...