C ++中嵌套类和多重继承之间的内存布局差异?
我试图了解 COM 如何指定其对象的布局,以便想要使用 COM 对象的客户端知道如何去做.
I am trying to understand how COM specifies the layout of its objects so that a client that wants to use a COM object knows how to do it.
我了解到,实现多个接口的 COM 对象可以通过不同的方式实现,包括使用嵌套类或多重继承.
I've read that a COM object that implements multiple interfaces can do it it in different ways including using nested classes or multiple inheritance.
我的理解是,这两种技术都必须产生相同的内存布局(符合 COM 规范),以便想要使用 COM 对象(例如在 C 中)的客户端知道如何去做.
My understanding is that both techniques would have to produce the same memory layout (conforming to the COM spec) so that a client that wants to use the COM object (for example in C), knows how to do it.
所以我的具体问题是:使用多重继承与嵌套类实现的 c++ 对象的内存布局是否存在差异.
So my specific question is: is there a difference in memory layout for c++ objects implemented using multiple inheritance versus nested classes.
有人可以指出指定 COM 对象布局的位置吗?
And could somebody point me to where a COM object layout is specified?
推荐答案
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.
相关文章