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

旧城冷巷雨未停 提交于 2019-12-01 23:32:47

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.

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.

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.

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!