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.

318 questions
2
votes
3 answers

STL-friendly pImpl class?

I am maintaining a project that can take a considerable time to build so am trying to reduce dependencies where possible. Some of the classes could make use if the pImpl idiom and I want to make sure I do this correctly and that the classes will…
Rob
  • 76,700
  • 56
  • 158
  • 197
2
votes
2 answers

Access Violation reading location 0xfeeefe2 on destructor call

This post will be a bit large, so sorry in advance. Anyway, I'm getting an exception when running my program in debug mode(Visual Studio 2010) that I can't quite figure why happens: Unhandled exception at 0x5524ad4a (msvcp100d.dll) in CppTest1.exe:…
Beepboop
  • 23
  • 1
  • 4
2
votes
1 answer

overlap between pimpl idiom and builder patterns?

I was reading about builder patters from the http://en.wikibooks.org/wiki/C%2B%2B_Programming/Code/Design_Patterns#Creational_Patterns link and the code below seems to use idea similar to pimpl idiom by having a pointer to pizzaBuilder in the Class…
vkaul11
  • 4,098
  • 12
  • 47
  • 79
2
votes
2 answers

C++ equivalently performant version of C-like implementation

I'm learning C++ while I run into this situation, where I want to implement an equivalently efficient version in C++ of the following symbolic code in C. struct Obj; Obj* create(...); void do_some_thing(Obj*); void…
el22
  • 21
  • 2
2
votes
3 answers

Is it a good idea to use private inheritance to hide implementation?

e.g. // Implementation. struct PrivatePoint { void SomePrivateMethod(); double x; double y; } struct Point : private PrivatePoint { double DistanceTo(const Point& other) const; } This seems similar to the Pimpl idiom. This has two…
allyourcode
  • 21,871
  • 18
  • 78
  • 106
2
votes
1 answer

QImage copy on write

is QImage based on QSharedData ? Do Qimage follow pimpl or copy on write ? e.g. would copying(through copy con or assignment) an Qimage make a deep copy of pixels ?
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
1
vote
2 answers

Best place to initialize default values in a pimpl class?

I make pretty extensive use of PImpl and something that I've found myself waffling on is where exactly to to initialize members of the Pimpl struct. Options are to create a constructor for the Private struct and initialize them there, or to…
Mike E
  • 846
  • 11
  • 22
1
vote
1 answer

Converting pImpl pointer back to caller type

I am using pimpl idiom in my program and I am stuck in one place. My code is Class* Class::GetP() { return ClassImpl->GetP(); } In my ClassImpl->GetP() I have ClassImpl* ClassImpl::GetP() { return pClassImpl; } As you can see I need to convert my…
Yogi
  • 1,035
  • 2
  • 13
  • 39
1
vote
1 answer

pimpl helper ambiguous with inheritance

I'm playing around with creating a utility class for the pimpl idiom, however I have some problem I hoped to get some help with: This is what I've got: [sehe: see also rev.1 here:…
ronag
  • 49,529
  • 25
  • 126
  • 221
1
vote
1 answer

Pimpl idiom through macro

I try to reduce code duplication in a class which implements the pimpl idiom. Imagine I have a header Foo.h. For better readability, I have reduced the methods. class FooImp; class Foo { public: void A(int x); void B(int x, double…
RoQuOTriX
  • 2,871
  • 14
  • 25
1
vote
2 answers

Can you use the pimpl idiom for use across multiple cpp files with one class?

For instance, here is what I mean. Let's say you have a single header file with a single pimpl class. Can you define the functions of this class across two cpp files without redefining the variables of the class? I've tried this before using a…
F35H
  • 13
  • 6
1
vote
1 answer

pImpl pattern in C++ needs complete definition of impl subclass

I've looked at many of the SO questions on pImpl, unique_ptr and forward declarations, but can't figure out what is wrong. The answer at Is std::unique_ptr required to know the full definition of T? seems very complete, but I can't figure out how…
GaryO
  • 5,873
  • 1
  • 36
  • 61
1
vote
0 answers

Using PIMPL but single header + multiple cpp files

I have a header file class.hpp that contains my main object class Class { public: Class(); ~Class(); private: struct Impl; std::unique_ptr impl; }; I then have my main implementation class_impl.cpp #include "Class.hpp" struct…
raaj
  • 2,869
  • 4
  • 38
  • 58
1
vote
0 answers

How can I implement the PIMPL idiom in a thread-safe manner?

I have a class that is implemented using the PIMPL idiom. A single instance of this class will be used across multiple threads within a multithreaded program. Inside the implementation of the class I made sure I did locking anywhere this class may…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
1
vote
1 answer

Max Heap built with pimpl in c++ not working properly

I have a class built using the pimpl idiom that represents a binary max Heap and it is not working properly: the program compiles and prints the content of the array but the array is not sorted correctly. In the examples I used in the main I should…
Spyromancer
  • 435
  • 1
  • 3
  • 10