c++ pimpl编程技法

之前写过一次关于pimpl的博文,最近写代码又频繁遇到这个,再次总结一次.

先来看一段代码,这段代码中无论Impl中的print函数如何更改,客端调用Shape的print接口都不用更改,因此降低了接口的耦合性.屏蔽掉了接口的具体实现方式.另一个作用就是降低了头文件之间的依赖关系,大大降低编译时间

#include <bits/stdc++.h>

using namespace std;

class Shape {
private:
    class Impl;
    Impl* impl;

public:
    Shape();
    void print();
};

class Shape::Impl {
public:
    void print()
    {
        cout << "shape" << endl;
    }
};

Shape::Shape()
    : impl(new Impl)
{
}

void Shape::print()
{
    impl->print();
}

int main()
{
    Shape s;
    s.print();
}

 

全部评论

相关推荐

06-11 17:39
门头沟学院 Java
小呆呆的大鼻涕:卧槽,用户彻底怒了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-24 13:40
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务