COM interface properties
this pointer as a COM interface pointer
The this
pointer of a C++ class instance points to the internal class object built by the C++ compiler. A C++ class with virtual functions contains a vtable
to access those functions. Because the vtable
is the first object in the C++ class object, the this
pointer is in effect a pointer to a pointer to a vtable
. That is, it has the same binary layout as a COM interface.
Why we have to use the corresponding interface pointer as the this parameter.
There are of course other means to do that, as long as you are the implementer of the component.
But this is not the case for the clients of our component. When the component is built using the COM way, clients of our component know nothing about the internals of our component. Clients can only take hold of the interface pointer, and this is the very pointer that will be passed into the interface
method as the this parameter. Under this expectation, the compiler has no choice but to generate the interface method's code based on this specific this pointer.
So the above reasoning leads to the result that:
It must be assured that each function in a vtable must recieve the corresponding interface pointer as its "this" parameter.
COM interface