I know that COM provides reusability at the binary level across languages and applications.
I read that all components built for COM must adhere to a standard memory layout in order to be language-independent.
I do not understand what “standard memory layout” means.
What makes COM language-independent?
First, some technical background: C++ compilers usually generate something called a “vtable” for any class with virtual functions. This is basically a table of function pointers. The vtable contains a function pointer to every virtual method implemented by a class.
In COM, interfaces are basically abstract base classes which a component implements, e.g.:
The vtable for
CSomeComponentwill include function pointers for all methods defined in these two interfaces.Any instantiated object has a reference to the vtable of its dynamic type. This is how the program knows how to call the proper method in cases where a base method is overridden in a derived class:
The last line should call
Derived::foo. This works because objectXhas a reference to the vtable for classDerived. As said, the vtable is like a list of function pointers. Now, vtables have a fixed layout: If classDerivedinherits from classBase, the function pointer for methodfoowill be at the same relative location inDerived‘s vtable than inBase‘s vtable:Now, if the compiler sees something like
X->foo(), it knows that all for all classes derived fromBase, methodfoocorresponds to the first entry in the vtable. So it issues a call to the first function pointer, which inX‘s case is a call toDerived::foo.Answer to your question: Compilers can only generate COM components if they generate the same layout for vtables that the COM specification demands. vtables can be implemented in various different ways, especially when it comes to multiple inheritance (which is required with COM components). Adhering to a certain vtable format is necessary so that when you call a component’s method
f, you will actually call methodfand not some other methodgwhich happens to sit atf‘s position in the component class’s vtable. I suppose COM-compliant compilers essentially have to produce the same vtable layouts as Microsoft Visual C++, since the COM technology was defined by Microsoft.P.S.: Sorry for being so technical, I hope the above information is of some use to you.