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
1
vote
1 answer

Comparing pimpl idiom with Microsoft COM

I know this topic is somewhat off-topic on StackOverflow. But I just cannot think of another more appropriate place to post it, and I really want to gather opinions from you guys. Recently, I come across the pimpl idiom. I think it is related to…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
1
vote
1 answer

How to incorporate a pImpl Interface while also allowing WndProc to interact with it?

Currently developing a 2-D game-dev environment with a Wrapper/GameEngine class combination in Win32 (C/C++ language). As it stands, I use the Wrapper to set up and initialize all items with the Window as well as to initialize the GameEngine class…
1
vote
0 answers

std::thread() and std::ref() and PIMPL: C++11 Thread Semantics

Below I have provided pseudo code for a situation I am encountering. I have two resources one that needs to be created on one thread and another that has to be created on the main thread. However, the first resource, ResourceA has to be recreated…
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140
1
vote
2 answers

std::unique_ptr for class data member ABI (Pimpl idiom)

I'm trying to define existing code that use "pimpl" data members to be defined with unique_ptr. Some objects requires a custom deleter, and others not. unique_ptr (unlike shared_ptr) destructor requires knowing a complete type of the object. So you…
Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
1
vote
1 answer

PImpl Doesn't Save Me from having to Export STL

I pursued the PImpl design to avoid having to export STL from my dynamic library. Old: //In header file class Foo{ public: const map& getMap() {return _map;} private: map _map; }; New: //In header file class…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
1
vote
3 answers

C++ nested class - moving implementation to different file

I'm trying to learn how to use the PIMPL idiom because it reduces compilation dependencies, which I've heard is recommended. So I have code that essentially looks like this. Foo.h class Foo { public: Foo(); private: class FooImpl; …
rcplusplus
  • 2,767
  • 5
  • 29
  • 43
1
vote
0 answers

Pimpl class definition in CPP file

Suppose I have a class X with private implementation Ximpl: //X.h class X { void foo(); //.. private: class Ximpl; std::unique_ptr x_impl; } Then there are several ways to structure the code. Perhaps the most common one is to put…
Oleg Shirokikh
  • 3,447
  • 4
  • 33
  • 61
1
vote
1 answer

Pass pimpl argument to a pimpl object

I've two classes that implement PIMPL: State and StateMachine. They implement Private::State and Private::StateMachine. Private::StateMachine has an "addState(Private::State &)" method. I want to write the addState() method into its PIMPL class, in…
Jepessen
  • 11,744
  • 14
  • 82
  • 149
1
vote
1 answer

Designing the instantiation and destruction of a class using the pimpl idiom

Note: I've rewritten the question to specify my intend clearer, and make it shorter. I'm designing a part of a library which has a few requirements: None of the implementation details must be visible from public headers. The memory must be managed…
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62
1
vote
1 answer

Error: forward declaration of ‘class SActionPrivate’ when using PIMPL

In Qt-program I realized the Pimpl approach, have 3 files saction.cpp saction.h saction_p.h - with a private class sactionPrivate The code is based on a code from kdelibs. I'm using CMAKE as a build system. So I get an error when…
Ivan Kush
  • 2,958
  • 2
  • 23
  • 39
1
vote
2 answers

Pimpl with multiple pointers

I'm working on a small IO library where the requirements for the interface are known, but the implementation is likely to change. The library is supposed to read and writes files into an archive format as well as store some metadata. I though about…
jaho
  • 4,852
  • 6
  • 40
  • 66
1
vote
1 answer

Cast Wrapper-Class to original class

I use Wrapper-Class using pimpl so I can use Objective-C methods while using C++. Now in my ViewWrapper.hpp I have this: class ViewWrapper { public: void addSubview(ViewWrapper *view); //method exists in original View.h (Objective-C) …
Southgarden116
  • 311
  • 3
  • 16
1
vote
2 answers

Opaque object for template in another namespace

I know how to do an opaque object in C++ as following: // my_class.hpp class opaque_object; class my_class { my_class(); ~my_class(); opaque_object *m_opaque_object; }; // my_class.cpp #include class opaque_object { …
Phong
  • 6,600
  • 4
  • 32
  • 61
1
vote
4 answers

Pimpl idiom: What size_type to use if implementation is unknown?

I have a class that holds an array of elements, and I want to give it a GetSize member function. But what return type should I give that function? I'm using the pimpl idiom, and so in the header file it is not known what the implementation will use…
Frank
  • 64,140
  • 93
  • 237
  • 324
1
vote
1 answer

VS2012 - Class Interface Design: Private Member Funcs Positioning / Hiding

In VS, when you type "class." you are presented with a list of functions you can call. Having to look through a list of 15-20 functions, half or more of which being members is not nice. I'm extremely interested in finding a system that will hide…
David
  • 1,050
  • 1
  • 16
  • 31