pImpl成语是否真的在实践中使用? [英] Is the pImpl idiom really used in practice?

查看:109
本文介绍了pImpl成语是否真的在实践中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Herb Sutter的书Exceptional C ++,在那本书中我学到了pImpl的用法。基本上,这个想法是为 private 对象创建一个结构,并动态分配给减少编译时间(并以更好的方式隐藏私有实现)。

I am reading the book "Exceptional C++" by Herb Sutter, and in that book I have learned about the pImpl idiom. Basically, the idea is to create a structure for the private objects of a class and dynamically allocate them to decrease the compilation time (and also hide the private implementations in a better manner).

例如:

class X
{
private:
  C c;
  D d;  
} ;

可以更改为:

class X
{
private:
  struct XImpl;
  XImpl* pImpl;       
};

,在CPP中,定义如下:

and, in the CPP, the definition:

struct X::XImpl
{
  C c;
  D d;
};

这似乎很有趣,但我从来没有见过这种方法,已经工作,也没有在开源项目中我看到源代码。所以,我想知道这种技术是真正在实践中使用吗?

This seems pretty interesting, but I have never seen this kind of approach before, neither in the companies I have worked, nor in open source projects that I've seen the source code. So, I am wondering it this technique is really used in practice?

我应该使用它无处不在,还是小心?这种技术推荐用于嵌入式系统(性能非常重要)。

Should I use it everywhere, or with caution? And is this technique recommended to be used in embedded systems (where the performance is very important)?

推荐答案


所以,我想知道这种技术是真正在实践中使用吗?

So, I am wondering it this technique is really used in practice? Should I use it everywhere, or with caution?

当然,它被使用,在我的项目中,几乎在每个类中,您提到的几个原因:

Of course it is used, and in my project, in almost every class, for several reasons you mentioned :


  • 数据隐藏

  • 重新编译时间确实减少,文件需要重建,而不是头文件和包含它的每个文件

  • 二进制兼容性。由于类声明没有改变,只更新库是安全的(假设您正在创建库)


这种技术推荐用于嵌入式系统(性能非常重要)?

is this technique recommended to be used in embedded systems (where the performance is very important)?

你的目标是多么强大。但是,这个问题的唯一答案是:测量和评估你获得和失去的。

That depends on how powerful your target is. However the only answer to this question is : measure and evaluate what you gain and lose.

这篇关于pImpl成语是否真的在实践中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆