“指向实现的指针”或“pImpl”是一种 C++ 编程技术[1],它通过将类的实现细节从其对象表示中移除,将它们放在一个单独的类中,通过一个不透明的指针访问
既:通过实例调用impl 屏蔽实现细节到impl文件夹
对外只提供.h屏蔽实现细节,也同时隐藏了.cpp中很多头文件引入,也常用在公共库开发中。
#include <iostream>
#include <memory>
#include <experimental/propagate_const>
// interface (widget.h)
class widget {
class impl;
std::experimental::propagate_const<std::unique_ptr<impl>> pImpl;
public:
void draw() const; // public API that will be forwarded to the implementation
void draw();
bool shown() const { return true; } // public API that implementation has to call
widget(int);
~widget(); // defined in the implementation file, where impl is a complete type
widget(widget&&); // defined in the implementation file
// Note: calling draw() on moved-from object is UB
widget(const widget&) = delete;
widget& operator=(widget&&); // defined in the implementation file
widget& operator=(const widget&) = delete;
};
// implementation (widget.cpp)
class widget::impl {
int n; // private data
public:
void draw(const widget& w) const {
if(w.shown()) // this call to public member function requires the back-reference
std::cout << "drawing a const widget " << n << '\n';
}
void draw(const widget& w) {
if(w.shown())
std::cout << "drawing a non-const widget " << n << '\n';
}
impl(int n) : n(n) {}
};
void widget::draw() const { pImpl->draw(*this); }
void widget::draw() { pImpl->draw(*this); }
widget::widget(int n) : pImpl{std::make_unique<impl>(n)} {}
widget::widget(widget&&) = default;
widget::~widget() = default;
widget& widget::operator=(widget&&) = default;
// user (main.cpp)
int main()
{
widget w(7);
const widget w2(8);
w.draw();
w2.draw();
}
官方例子