1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef PIMPL_H #define PIMPL_H #include <memory>
class pImpl { struct Impl; std::unique_ptr<Impl> impl; public: pImpl(); ~pImpl(); void dosomething(); };
#endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include "pImpl.h"
#include <iostream>
struct pImpl::Impl { int a,b,c,d; void dosomething() { std::cout << "dosomething1" << std::endl; } };
pImpl::pImpl() : impl(new Impl) {} pImpl::~pImpl() = default; void pImpl::dosomething() { impl->dosomething(); }
|
1 2 3 4 5
| #include"pImpl.h" int main() { pImpl p; p.dosomething(); }
|
更改这个Impl类的时候就不用再重新构建一遍main,因为pImpl.h完全没变