Guru of the Week 条款24:编译级防火墙

本文围绕C++编译级防火墙展开,介绍了利用不透明指针隐藏实现细节的pimpl惯用法。探讨了将哪些部分放入Ximpl的四种常见原则及优缺点,还讨论了Ximpl是否需要指向X对象的反向指针,同时指出使用该惯用法的主要代价是性能。

[CAT*G Translation Project GotW#22-30: Draft]

GotW #24 Compilation Firewalls
著者:Herb Sutter
翻译:CAT*G
[声明]:本文内容取自www.gotw.ca网站上的Guru of the Week栏目,其著作权归原著者本人所有。译者CAT*G在未经原著者本人同意的情况下翻译本文。本翻译内容仅供自学和参考用,请所有阅读过本文的人不要擅自转载、传播本翻译内容;下载本翻译内容的人请在阅读浏览后,立即删除其备份。译者CAT*G对违反上述两条原则的人不负任何责任。特此声明。
Revision 1.0

Guru of the Week 条款24:编译级防火墙

难度:6 / 10

(使用pimpl惯用法可以大大降低代码之间的相互依赖性,还可以减少程序的建立时间。但问题是,应该把那些东西放入pimpl对象里呢?如何才能安全的使用它呢?)

[Problem]
[问题]

  在C++中,如果类定义中的任何部分被改变了(即使是私有成员),那么这个类所有的使用者代妈都必须重新编译。为了降低这种依赖性,使用的一种常见的技术就是利用一个不透明指针(opaque pointer)来隐藏一部分实现细节:

    class X {
    public:
      /* ... 公有成员 ... */
    protected:
      /* ... 保护成员?... */
    private:
      /* ... 私有成员?... */
      class XImpl* pimpl_;  // 指向一个被前置声明了的(forward-declared)类
                                // 之不透明指针
    };

[Questions]
[提问]

1.那些部分应该放入Ximpl?有四种常见的原则,它们是:

    - 将全部私有数据(但不是函数)放入Ximpl;
    - 将全部私有成员(译注:即包括函数)放入Ximpl;
    - 将全部私有成员和保护成员放入Ximpl;
    - 使Ximpl完全成为原来的X,将X编写为一个完全由简单的前置函数(forwarding functions)(一个句柄/本体的变体)组成的公共接口。

它们各有什么优缺点?你如何从中选择合适的?

2.Ximpl需要一个指向X对象的"反向指针(back pointer)"吗?

[Solution]
[解答]

首先看两个定义:

  可见类(visible class):客户代吗所见并操纵的类(在这里即是X)。
  pimpl:可见类中隐藏在一个透明指针(也称为pimpl_)下的类实现(在这里即是XImpl)。

  在C++中,如果类定义中的任何部分被改变了(即使是私有成员),那么这个类所有的使用者代妈都必须重新编译。为了降低这种依赖性,使用的一种常见的技术就是利用一个不透明指针(opaque pointer)来隐藏一部分实现细节:

  这是"句柄/本体"惯用法(handle/body idiom)的一种变体。如Coplien[注1]所记载,这种方法主要用于在共享代码的情况下进行引用计数(

Xfilter 源代码完整的文件列表 ------------------------------------------------------ .\Common .\Lib .\Property .\Release .\TcpIpDog .\Xfilter.dsw .\readme.txt .\filelist.txt .\Common\XLogFile.h .\Common\XLogFile.cpp .\Common\XInstall.cpp .\Common\XFile.h .\Common\XInstall.h .\Common\XFile.cpp .\Common\Debug.h .\Common\XFileRes.h .\Lib\htmlhelp.h .\Lib\htmlhelp.lib .\Property\Property.opt .\Property\Property.aps .\Property\Property.dsp .\Property\Property.rc .\Property\Property.clw .\Property\ReadMe.txt .\Property\resource.h .\Property\NetIPAria.h .\Property\GuiRes.h .\Property\AclSet.cpp .\Property\SetTime.h .\Property\Acl.cpp .\Property\SetNet.h .\Property\MainSheet.h .\Property\AclSet.h .\Property\Register.h .\Property\SystemSet.cpp .\Property\Splash.h .\Property\Property.dsw .\Property\Acl.h .\Property\Property.ncb .\Property\Splash.cpp .\Property\Property.h .\Property\LogQuery.h .\Property\SetTime.cpp .\Property\MainSheet.cpp .\Property\SetNet.cpp .\Property\NetIPAria.cpp .\Property\About.cpp .\Property\StdAfx.h .\Property\StdAfx.cpp .\Property\SystemSet.h .\Property\Register.cpp .\Property\About.h .\Property\LogQuery.cpp .\Property\Property.cpp .\Property\NetTimeSheet.h .\Property\PacketMonitor.h .\Property\Property.plg .\Property\NetTimeSheet.cpp .\Property\PacketMonitor.cpp .\Property\Internet .\Property\MainFrame .\Property\SystemTray .\Property\res .\Property\HyperLink .\Property\Internet\Internet.cpp .\Property\Internet\Internet.h .\Property\MainFrame\MainFrame.cpp .\Property\MainFrame\mainframe.h .\Property\SystemTray\SystemTray.cpp .\Property\SystemTray\SystemTray.h .\Property\res\Property.rc2 .\Property\res\NULL.ico .\Property\res\Property.ico .\Property\res\about.bmp .\Property\res\Alert.ico .\Property\res\DenyEx1.ico .\Property\res\PassEx1.ico .\Property\res\QueryEx1.ico .\Property\res\splash.bmp .\Property\res\MEMO.ICO .\Property\res\ALERTSET.ICO .\Property\res\APPSET.ICO .\Property\res\BASESET.ICO .\Property\res\COMMONSET.ICO .\Property\res\Monitor.ico .\Property\res\NETSET.ICO .\Property\res\SUPERSET.ICO .\Property\res\TIMESET.ICO .\Property\res\Xfilter.ico .\Property\res\IPSET.ICO .\Property\res\Email.ico .\Property\res\QueryResult.ico .\Property\res\QuerySet.ICO .\Property\res\UserInfo.ico .\Property\res\ACLSET.ICO .\Property\res\Message.ico .\Property\HyperLink\HyperLink.cpp .\Property\HyperLink\HyperLink.h .\Release\xacl.cfg .\Release\Xfilter.chm .\Release\Xfilter.exe .\Release\Xfilter.dll .\Release\xlog.dat .\TcpIpDog\StdAfx.cpp .\TcpIpDog\TcpIpDog.dsp .\TcpIpDog\LspServ.def .\TcpIpDog\ReadMe.txt .\TcpIpDog\CheckAcl.cpp .\TcpIpDog\TcpIpdog.cpp .\TcpIpDog\Codes.h .\TcpIpDog\TcpIpDog.h .\TcpIpDog\ProtocolInfo.h .\TcpIpDog\CheckAcl.h .\TcpIpDog\StdAfx.h .\TcpIpDog\ProtocolInfo.cpp .\TcpIpDog\TcpIpDog.plg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值