概念:指向实现对象的指针,在头文件中,对类进行声明,将其中一些成员变量用一个具体实现类或者结构体的指针来代替,然后将这个具体实现类或结构体的定义放在源文件中
作用:
- 声明与定义分离,对用户隐藏实现
- 加快编译速度
例子:
// a.h
class A{
public:
A():
private:
std::map<std::string, std::string> name_map_;
std::vector<double> data_;
};
// a.h
class A{
public:
A();
private:
struct Tmpl;
std::unique_ptr<Tmpl> p_impl_;
};
// a.cc
#include "a.h"
struct A::Tmpl{
std::map<std::string, std::string> name_map_;
std::vector<double> data_;
};
A::A() : p_impl_(std::make_unique<Tmpl>()) {};