Questions tagged [pimpl-idiom]

The PIMPL idiom, also known as the compilation firewall or Cheshire Cat technique, is a "private implementation" technique useful in C++ and other similar statically compiled languages.

The idiom makes use of an opaque pointer to another object (usually of a nested class type) which provides the implementation. Member functions on the outer object forward to the implementation object, which is defined in a separate source file so that the implementation is not visible in the header defining the outer class. This is a special case of the Bridge pattern.

The term Cheshire Cat (because the body disappears leaving only a smile) is older, but the more common name PIMPL idiom was popularized by Herb Sutter in GOTW #24 (and revisited in GOTW #100 and #101).

Links:

Separating Interface and Implementation in C++ compares the PIMPL idiom to other related techniques.

281 questions
8
votes
6 answers

What patterns do you use to decouple interfaces and implementation in C++?

One problem in large C++ projects can be build times. There is some class high up in your dependency tree which you would need to work on, but usually you avoid doing so because every build takes a very long time. You don't necessarily want to…
Tobias
  • 5,950
  • 3
  • 33
  • 60
8
votes
4 answers

Is there any way to limit repetitive boilerplate when using the PIMPL idiom?

I have something like the following: // foo.h: class foo { public: foo(); ~foo(); // note: the param type repetition here is only incidental, assume the // functions can't easily be made to share type signatures void bar(a b, c…
Claudiu
  • 206,738
  • 150
  • 445
  • 651
8
votes
2 answers

Pimpl + QSharedPointer - Destructor = Disaster

Yesterday I ran into misery which took me 24 hours of frustration. The problem boiled down to unexpected crashes occurring on random basis. To complicate things, debugging reports had absolutely random pattern as well. To complicate it even more,…
Alexander Shukaev
  • 15,556
  • 8
  • 64
  • 81
7
votes
1 answer

What does "d" stand for in d-pointer?

Qt makes heavy use of the PIMPL idiom in their development process: https://wiki.qt.io/D-Pointer As I've read here: "The name 'd-pointer' stems from Trolltech's Arnt Gulbrandsen, who first introduced the technique into Qt, making it one of the first…
Jacob Krieg
  • 2,430
  • 10
  • 60
  • 114
7
votes
3 answers

C++: Tool to reduce compile-time dependencies automatically

After reading about the pimpl idiom I was horrified! Isn't there a tool out there that can inspect a .h/.cpp file and deduce what dependencies could be waivered?
Jonathan
  • 84,911
  • 94
  • 244
  • 345
7
votes
2 answers

keeping private parts outside c++ headers: pure virtual base class vs pimpl

I recently switched back from Java and Ruby to C++, and much to my surprise I have to recompile files that use the public interface when I change the method signature of a private method, because also the private parts are in the .h file. I quickly…
skrebbel
  • 9,557
  • 6
  • 32
  • 34
7
votes
3 answers

Why should a pimpl be declared as a struct and not a class?

The canonical form of the pimpl idiom (from Herb Sutter's "Exceptional C++") is as follows: class X { public: /* ... public members ... */ protected: /* ... protected members? ... */ private: /* ... private members? ... */ struct XImpl; …
lindelof
  • 31,966
  • 30
  • 87
  • 133
7
votes
2 answers

Complete encapsulation without malloc

I was experimenting with C11 and VLAs, trying to declare a struct variable on the stack with only an incomplete declaration. The objective is to provide a mechanism to create a variable of some struct type without showing the internals (like the…
Mabus
  • 1,208
  • 9
  • 16
7
votes
4 answers

Is it possible to write an agile Pimpl in c++?

I've been playing with the Pimpl idiom and reaping all sorts of benefits from it. The only thing I haven't been too keen on is the feeling I get when I define the functions. Once in the header (P def) Once at the top of the .cpp (Impl def) Once…
user1684306
6
votes
3 answers

pimpl-idiom in template; which smart pointer?

I usually use a boost::scoped_ptr for pimpl's (for one reason because then I don't get surprises if I forget to deal with the copy constructor) With templates however I can't just put the destructor in the cpp file where the impl is fully defined…
odinthenerd
  • 4,964
  • 1
  • 22
  • 53
6
votes
5 answers

C++ advice from Code Complete on encapsulation?

In the section on "Good Encapsulation" in Code Complete, it is recommended to hide private implementation details. An example is given in C++. The idea is basically to completely separate the interface from the implementation, even in the class…
voithos
  • 60,391
  • 10
  • 90
  • 110
6
votes
7 answers

Hiding a C++ class in a header without using the unnamed namespace

I am writing a C++ header in which I define a class A { // ... }; that I would like to hide from the outside world (because it may change or even be removed in future versions of this header). There is also a class B in the same header that has…
Bjoern
  • 63
  • 1
  • 3
6
votes
5 answers

Is pimpl compatible with anonymous namespaces?

I am trying to use the pimpl pattern and define the implementation class in an anonymous namespace. Is this possible in C++? My failed attempt is described below. Is it possible to fix this without moving the implementation into a namespace with a…
anatolyg
  • 23,079
  • 7
  • 51
  • 113
6
votes
4 answers

Putting all methods in class definition

When I use the pimpl idiom, is it a good idea to put all the methods definitions inside the class definition? For example: // in A.h class A { class impl; boost::scoped_ptr pimpl; public: A(); int foo(); } // in A.cpp class…
Amnon
  • 7,202
  • 2
  • 24
  • 34
6
votes
2 answers

Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to ".cpp"?

The code would work file as long as I don't move the definition of constructor (of B) to the header B.h. B.h class Imp; //<--- error here class B{ public: std::unique_ptr imp; B(); //<--- move definition to here will compile…
javaLover
  • 6,039
  • 2
  • 14
  • 57
1 2
3
18 19