Is the pimpl idiom used in c#?
Asked Answered
D

3

17

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.

Demarcusdemaria answered 26/4, 2012 at 17:44 Comment(2)
It's not "necessary" in C++, it's merely there to save on build times (and in some cases to maintain binary compatibility). If C++ compiled/linked as fast as C# compiles, I doubt anyone would bother.Impeditive
You may find Eric Lippert's post, How Many Passes, of interest.Dasilva
D
27

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!

Depreciable answered 26/4, 2012 at 18:7 Comment(4)
What benefit is there to creating a private nested class, as opposed to skipping the private nested class but still using a private constructor and factory method? Couldn't you hide the implementation details by just making the relevant members and fields private?Dasilva
@Brian: Sure, if you have a trivial class that is entirely self-contained then do it all in one class. But what if you have a base class BankAccount and derived classes SavingsAccount and ChequingAccount, and then more derived classes from them... and you want all the classes except for the base class to be private implementation details of BankAccount? You could make them internal to an assembly, or you could make them private to a type. The latter emphasizes to your coworkers that these classes are private implementation details, not fair game for re-use.Depreciable
Well, there are some efforts to re-think the compilation model for ANSI C++ see "C++ modules". However, the outcome is at least uncertain. If you're a ANSI C++ library writer, you either do clean modern C++ (and publish source code), or you do "practical" (deployable, reusable, protectable) C++ which is at it's boundaries full of "hacks" and not ANSI any more...Titicaca
And, of course, you could combine the nested private class technique with partial classes, to keep the files small and readable and make it easier for developers to work concurrently.Averil
T
4

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.

Titicaca answered 26/4, 2012 at 17:52 Comment(0)
P
1

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

Periphrasis answered 26/4, 2012 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.