建立网站软件,自己的服务器如何做网站,如何建立门户网站,数据库用于网站建设哪个好C 中的 Pimpl 惯用法
介绍
Pimpl#xff08;Pointer to Implementation#xff09;是一种常见的 C 设计模式#xff0c;用于隐藏类的实现细节#xff0c;从而减少编译依赖和提高编译速度。本文将通过一个较为复杂的例子#xff0c;展示如何使用智能指针#xff08;如 s…C 中的 Pimpl 惯用法
介绍
PimplPointer to Implementation是一种常见的 C 设计模式用于隐藏类的实现细节从而减少编译依赖和提高编译速度。本文将通过一个较为复杂的例子展示如何使用智能指针如 std::unique_ptr来实现 Pimpl 惯用法。
什么是 Pimpl 惯用法
Pimpl 是 “Pointer to Implementation” 的缩写这个模式可以帮助我们
将接口和实现分离减少头文件中的依赖加速编译
基本实现
基本的 Pimpl 实现需要一个前置声明的内部类和一个指向该内部类的指针。在这里我们使用 std::unique_ptr 来管理这个内部类的实例。
MyClass.h
#include memoryclass MyClassImpl; // 前置声明class MyClass {
public:MyClass();~MyClass();void DoSomething();private:std::unique_ptrMyClassImpl pimpl;
};MyClass.cpp
#include MyClass.hclass MyClassImpl {
public:void DoSomething() {// 实现细节}
};MyClass::MyClass() : pimpl(std::make_uniqueMyClassImpl()) {}
MyClass::~MyClass() default;void MyClass::DoSomething() {pimpl-DoSomething();
}示例
假设我们有一个 Car 类它有多个组件如 Engine 和 Wheel。
Car.h
#include memory
class CarImpl;class Car {
public:Car();~Car();void Start();void Stop();private:std::unique_ptrCarImpl pimpl;
};Car.cpp
#include Car.h
#include Engine.h
#include Wheel.hclass CarImpl {
public:Engine engine;Wheel wheel[4];void Start() {engine.Start();// 其他逻辑}void Stop() {engine.Stop();// 其他逻辑}
};Car::Car() : pimpl(std::make_uniqueCarImpl()) {}
Car::~Car() default;void Car::Start() {pimpl-Start();
}void Car::Stop() {pimpl-Stop();
}在这个例子中Car 的用户只需要包含 Car.h而不需要知道 Engine 和 Wheel 的存在。这样就降低了编译依赖并提高了编译速度。
总结
通过使用 Pimpl 惯用法和智能指针我们能更有效地隐藏实现细节提高编译速度并使代码更易于维护。