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库有兴趣,看看 ...
随机推荐
- [Django] 查看orm自己主动运行的原始查询sql
django的文档看了非常多.也用了不少,有的时候感觉性能非常不好,知道非常多地方是惰性查询.可是对于复杂的逻辑.仅仅是表面上发现执行非常慢,机器资源消耗非常多.却不知道orm究竟是什么来转化成sql ...
- 怎么设置MySQL就能让别人访问本机的数据库了?
for all ips use below GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION; for particular ...
- SMI#、SCI#信号在OS、BIOS、EC中的中断方式(Linux)
EC资料有个很好的CSDN博客推荐去看看:zhao_longwei的专栏 EC固件代码架构:https://blog.csdn.net/zhao_longwei/article/details/510 ...
- Thunderbolt雷电接口
官网:https://thunderbolttechnology.net/tech/certification
- sql time 比较
数据字段为varchar类型的,格式:20110228 151010想进行时间比较,搜索一个范围内的时间select * from table where ' 20120102' <`time ...
- windows下taskkill命令简介
1.简介 使用该工具可以按照进程 ID (PID) 或映像名称终止任务. 2.语法 TASKKILL [/S system [/U username [/P [password]]]] ...
- kubernetes之常见故障排除(一)
系列目录 由由种种原因,在安装或者使用kubernetes的过程中,可能会遇到各种各样的问题.本篇按照官网的介绍罗列出一些常见的故障,以帮助快速解决一些常见的错误. 安装赛程中出现ebtables o ...
- vue directive demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- switch多分枝语句
package lianxi; //switch多分枝语句 import java.util.Scanner; public class GetSwitch { public static void ...