I come from a C# background and have recently started learning C++. One of the things I've encountered is the pimpl idiom. I've done C# development for some large firms, but never came across it.
Maybe this is wrong, but my understanding is that it's necessary in C++ because of the use of header files and no partial class option.
But in C# we would build applications using class libraries all the time. If something changed in the library code, we would recompile it to a dll and reference the new dll in the application project.
I don't really understand why the same thing can't be done with C++. Pimpl just looks like an ugly hack to me.
The PImpl Idiom (Pointer to IMPLementation) is a technique used for separating implementation from the interface. It minimizes header exposure and helps programmers to reduce build dependencies by moving the private data members in a separate class and accessing them through an opaque pointer.
The pimpl idiom is a modern C++ technique to hide implementation, to minimize coupling, and to separate interfaces. Pimpl is short for "pointer to implementation." You may already be familiar with the concept but know it by other names like Cheshire Cat or Compiler Firewall idiom.
Is the pimpl idiom used in c#?
That depends on what you mean by this idiom.
The idiom in question is essentially to separate the implementation details of a type into one class, and the public surface area into a wrapper class that simply holds onto a pointer to the implementation class.
This has two benefits.
First, in C++ it can lead to improvements in compile time because consumers of the public surface area need only to parse the header file containing the public surface area; they need not parse the header file containing the implementation details.
Second, it leads to a very clean separation of interface from implementation; the implementation can change utterly without there ever being any impact upon the consumer, because the consumer never sees the implementation details.
The first benefit is of no matter in C#. The C# compiler is fast. (In large part of course because the language was designed to be fast to compile.)
The second benefit is of considerable use in C#. In C# the idiomatic way to implement this pattern would be to make a private nested class that does the "real" work and a public class that is just a facade with the public surface area.
A technique I particularly like in C# is to make the public class the base class of the private nested class, give the base class a private constructor to prevent third party extension, and use the factory pattern to hand out instances of the private nested class.
I don't really understand why the same thing can't be done with C++.
Then I encourage you to attempt to write a C++ compiler that does so. You'll either succeed in creating a much faster C++ compiler, or you'll find out why the same thing cannot be done in C++. You benefit either way!
Yes it is (a "hack"). It is caused by a text-based compilation model for ANSI C++: the compiler needs to see the textual representation of the code in order to generate something useful. Not that it has to be that way, but that's how it is done today.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With