且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++中嵌套类和多重继承之间的内存布局差异?

更新时间:2023-09-23 19:39:28

COM 完全不知道对象的内存布局.当它调用 IUnknown::QueryInterface() 时,它想要和需要的只是一个函数指针表.如何实现它完全取决于您.MFC 使用嵌套类,几乎所有其他东西都利用了 C++ 编译器中对多重继承的内置支持.MSVC++ 编译器实现它的方式与 COM 需要的完全兼容.这绝非偶然.使用您在有关 COM 的书籍中看到的样板代码,这些代码展示了如何正确实现 IUnknown.

COM is completely agnostic of the memory layout of your object. All that it wants and needs is a table of function pointers when it calls IUnknown::QueryInterface(). How you implement it is completely up to you. MFC uses nested classes, just about anything else leverages the built-in support for multiple inheritance in the C++ compiler. The way the MSVC++ compiler implements it is completely compatible with what COM needs. This is no accident. Use the boilerplate code you see listed in books about COM that shows how to properly implement IUnknown.