C++中的PImpl Idiom的简介
[TOC]
在《effective C++》中了解到了PImpl Idiom模式, 搜索了解了一下, 有一篇英文介绍简洁易懂, 遂分享出来, 原文.
PImpl Idiom in C++ with Examples
背景描述:
只要被包含的头文件出现哪怕多小的变动, 所有依赖于它的文件都必须要重新编译, 效率低下, 解决这一问题的一个方案为 PImpl Idiom.
概念介绍:
PImpl Idiom 是一种接口与实现分离的技术. 它通过把 private data members 移动到分离的类中, 然后通过指针访问它, 来实现最小地暴露头文件减少构建依赖.
如何实施方案 How to implement:
- 创建一个新 class ( or struct ).
- 把原类中所有的 private members 放入新类中.
- 在头文件中定义一个指向新类的指针(智能指针是不错的选择).
- 在头文件中前向声明新类/指向新类的指针.
- 定义好新类里的析构函数,copy 构造函数,assignment operator.
明确定义析构/ copy 构造函数/ assignment operator 的原因: 方便智能指针( 例如 std::unique_ptr
)检查可见的析构函数, 以及对于无法处理 copy /赋值的std::unique_ptr
而言, copy 构造函数/ assignment operator 也是必不可少的.
实例 Example:
User class 仅为接口以及数据结构体智能指针. 因此实现的任何变化只会影响 User.cpp, 其他用到 User class 的不需要重新编译.
1 | /* |INTERFACE| User.h file */ |
1 | /* |IMPLEMENTATION| User.cpp file */ |
优点 Advantages of PImpl:
- 二进制兼容性: 二进制接口与私有数据段独立, 改变实现的数据不会破坏依赖其的代码.
- 编译时间: 前面介绍的不用重新编译.
- 数据隐藏: 接口与实现独立的必然结果.
缺点 Disadvantages of PImpl:
- 内存管理: 增加内存使用量大小.
- 维护量增加: 复杂了, 维护起来费力一些.
- 继承性: 被隐藏起来的实现无法被继承, 接口虽然可以被继承.
C++中的PImpl Idiom的简介
https://www.chuxin911.com/PImpl_Idiom_in_C++_with_Examples_20211122/