Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a COM object a C++ class?

Tags:

c++

c

com

I've read two articles describing COM objects as being basically C++ classes. Is this true? Here's one of the articles I am reading: http://www.codeproject.com/Articles/13601/COM-in-plain-C

It describes a means for creating a COM object in C. COM enforces that the first members of this structure are certain function pointers.

I am not seeing the concrete correlation between COM objects and C++, although I can understand that relationship is easily understood.

like image 426
LunchMarble Avatar asked Jun 14 '26 05:06

LunchMarble


2 Answers

COM specifies exactly how its objects are to be laid out: with the virtual function table pointer (aka vtable pointer) as the first member in the object. It's designed so that the layout is identical to how the Microsoft compiler lays out C++ objects, so that you can have a C++ class that compiles identically to a manually defined C structure.

As long as you're using a Microsoft compiler, then yes, a COM object is basically a C++ class instance. But if you're using a different compiler, you need to take special care to ensure that it lays out C++ objects in the same manner. As long as you're not using multiple inheritance or virtual inheritance, most compilers will put the vtable first, so it will usually work, but some compilers put the vtable at the end. Make sure to read your compiler's documentation and figure out if it's COM-compatible or not.

like image 79
Adam Rosenfield Avatar answered Jun 16 '26 17:06

Adam Rosenfield


IMO, this depends a little on how you define a COM object. If a COM object is any object that is compatible with the COM Binary Interface, then whether or not the object is internally implemented in C++ is an implementation detail. It can be implemented in C++, but it could also be implemented in all sorts of other ways, and it is largely irrelevant as long as you access it through the COM interface.

That being said, as Adam and WhozCraig pointed out, COM's Binary Interface was designed with MSVC's C++ compiler in mind. Writing COM objects in MSVC C++ was the 'original' way to create COM objects. So the two definitely have an interwoven history, and indeed the COM Binary Interface specification has a lot in common with how MSVC lays out C++ objects. But, the MSVC compiler still has to generate COM bindings as an 'extra' compile step (e.g., the COM typelib, and C++ import headers so other C++ projects can import the COM library).

like image 31
WeirdlyCheezy Avatar answered Jun 16 '26 18:06

WeirdlyCheezy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!