COM复用的中Containment(包容)和Aggregation(聚合)的实现

本文详细介绍了COM复用中的Containment和Aggregation的概念及实现方式,通过具体代码展示了如何在Component A中实现这两种复用方法。在Aggregation中,重点讨论了NondelegationUnknown和DelegationUnknown接口的使用,以及在MFC中如何利用接口映射表简化COM编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Containment是一种比较简单的复用方法,如果Component B复用Component A,Component B实际上是Component A的一个客户,Component B向客户提供的Component A的功能实际上是Component B直接调用Component A完成的。当然Component B可以扩充Component A的功能。Component B可以直接使用已经存在的Component A,而不需要对Component A做任何改动
  2. Containment的例子实现略(潘爱民《COM原理与应用》第四章)
  3. Aggregation则比较复杂,Component A必须能够适应被Aggregation下的特殊处理。其核心在于QueryInterface函数。Aggregation涉及到聚合对象和被聚合对象双方的协作,体现了真正意义上的COM复用,而Containment只是客户程序和Component的嵌套这是Containment和Aggregation的本质区别
  4. Aggregation的实现(摘自潘爱民《COM原理与应用》)。
    Component A的Code
    class INondelegatingUnknown
    {
    public:
           virtual HRESULT  __stdcall  NondelegationQueryInterface(const IID& iid, void **ppv) = 0 ;
           virtual ULONG       __stdcall  NondelegatingAddRef() = 0;
           virtual ULONG       __stdcall  NondelegationRelease() = 0;
    };
    class CA : public ISomeInterface, public INondelegatingUnknown
    {
    protected:
         ULONG           m_Ref;
    public:
         CA(IUnknown *pUnknownOuter);
         ~CA();
    public :
           // Delegating IUnknown
           virtual HRESULT      __stdcall  QueryInterface(const IID& iid, void **ppv) ;
           virtual ULONG         __stdcall  AddRef() ;
           virtual ULONG         __stdcall  Release() ;
           // Nondelegating IUnknown
           virtual HRESULT   __stdcall  NondelegationQueryInterface(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值