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库有兴趣,看看 ...
随机推荐
- CNN网络--VGGNet
Simonyan, Karen, and Andrew Zisserman. "Very deep convolutional networks for large-scale image ...
- vscode格式化代码无效--可能的解决方法
因为vscode默认启用了根据文件类型自动设置tabsize的选项,因此,可以通过关闭自动设置选项,防止格式覆盖.在用户设置里添加如下配置即可: "editor.detectIndentat ...
- 《从0到1》读书笔记第一章"未来的挑战"第2记:做老子还是做孙子
从1到N VS 从0到1 - 别让自己的小鸡鸡抓在别人的手上 近几年国内互联网创业上非常流行一种C2C(也就是Copy to China - 复制到中国)的创业模式,打的就是一个时间差和地域差.将在国 ...
- Android系统开发(6)——Linux底层输入输出
一.操作系统的体系结构 计算机是由一堆硬件组成的,操作系统是为了有效的控制这些硬件资源的软件.操作系统除了有效地控制这些硬件资源的分配.并提供计算机执行所须要的功能之外,为了提供程序猿更easy开发软 ...
- windows下redis安装以及简单配置
1.下载redis 下载地址https://github.com/dmajkic/redis/downloads.有32bit和64bit根据自己需要选择就可以了. 2.安装redis 首先使用cmd ...
- Python生成8位随机字符串的一些方法
#第一种方法 import random import string seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP ...
- Ubuntu 登陆异常-输入正确的密码后还会返回到登陆界面的问题
问题表现: 启动到了登陆界面,输入对应的密码,发现一闪黑屏有返回到登陆界面,如此往复. 解决方法: 开机后在登陆界面按下shift + ctrl + F1进入tty命令行终端登陆,可以查看用户主目录下 ...
- Matlab---傅里叶变换---通俗理解(二)
1.用Matlab进行傅立叶变换 FFT是离散傅里叶变换的高速算法,能够将一个信号变换到频域.有些信号在时域上是非常难看出什么特征的,可是假设变换到频域之后,就非常easy看出特征了.这就是非常多信号 ...
- kubernetes的Service Account和secret
系列目录 Service Account Service Account概念的引入是基于这样的使用场景:运行在pod里的进程需要调用Kubernetes API以及非Kubernetes API的其它 ...
- JS 省市两级联动(不带地区版本)
基于网上找的一个版本改造,因为项目需求不需要地区只要省.市,所以做了改版,两个input上直接取出了数据 <html> <head> <script src=" ...