Question

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.

Was it helpful?

Solution

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!

OTHER TIPS

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.

As Eric pointed out, the pimpl idiom has two purposes. I would only add that the second purpose (separating interface and implementation) is one of the well known software design patterns: Bridge (also known as the Handle/Body I think). You can read more about it here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top