When implementing several COM interfaces at once how do I upcast to IUnknown?

后端 未结 4 1207
野性不改
野性不改 2021-01-21 23:43

Suppose my COM object implements two or more COM interfaces:

class CMyClass : public IPersistFile, public IPersistStream {
};

when implementing

相关标签:
4条回答
  • 2021-01-22 00:12

    IUknown is unimplemented. You need to provide all the implementation of IUnknown. Thus, to QI IUknown, you return the this pointer.

    AddRef, Release and QI are all implemented by you and not on the parent interface anyway so you have no issues just CALLING addref, no casting is required.

    0 讨论(0)
  • 2021-01-22 00:27

    Mark Ransom already gave the correct answer - any will do, as long as it's consistent - but picking the first one has one minor advantage. Due to layout rules, the IUnknown* of the first interface will point to the start of the object. Any other IUnknown* will point to subsequent vtable pointers elsewhere in the object. For debugging purposes, it's very useful to know where ano object begins in memory.

    0 讨论(0)
  • 2021-01-22 00:30

    It doesn't matter which upcast you use, only that you use the same one always. I'd just pick a convention, such as always returning the first one declared in the inheritance list.

    0 讨论(0)
  • 2021-01-22 00:31

    Usually in cases where you want to cast to IUnknown an object with multiple inheritence from IUnknown, you cast it to one of its interface then cast to IUnknown...

    0 讨论(0)
提交回复
热议问题