I know this topic is somewhat off-topic on StackOverflow. But I just cannot think of another more appropriate place to post it, and I really want to gather opinions from you guys.
Recently, I come across the pimpl idiom. I think it is related to Microsoft's Component Object Model (COM), and trying to make a comparison of the two. Such a comparison should be beneficial to the understanding, and I want to make sure that I get it right. So, if you find any misunderstanding in the comparison or have any additions to make, feel free to post it as an answer. The comparison is as follows:
Similarities:
- Both of the two decouple interface from implementation at the binary level. Therefore, changing the implementation does not require recompiling user code.
Differences:
Since COM interfaces use virtual functions only, user code can be compiled and linked independently without referencing the implementation in any way. This enables the binary module (i.e.,
.dll
on Windows and.so
on Linux) of the implementation to be dynamically loaded at demands during process running (e.g., through explicit calls toLoadLibrary()
on Windows platforms). With the pimpl idiom, class methods are not necessarily virtual, and user code linking has to reference the implementation, or there will be unresolved external symbol references. Besides, loading of the implementation binary module has to be done implicitly at process startup by the operating system loader.The pimpl idiom allows instantiating implementation objects directly in user code. With COM, however, a factory function has to be used to obtain a pointer to the implementation object.
- While COM is designed to be compiler-neutral, the pimpl idiom is not because of name-mangling and such.