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
2 answers

PIMPL idiom for a pointer to a class in C++

I have a working interface for two programs (ProgramA and ProgramB) that I would like to improve decoupling both programs as much as possible. The case that I want to cover is making a call from ProgramA to a class from ProgramB (Compute_Prop) that…
Zythos
  • 180
  • 2
  • 8
1
vote
2 answers

invalid use of incomplete type error in pimpl idiom

foo.h #include"fooImpl.h" class foo { private: class fooImpl; std::unique_ptr foo_imple; public: void bar(); }; fooImpl.h #include "foo.h" class foo::fooImpl …
Shibir Basak
  • 101
  • 7
1
vote
1 answer

Multiple Pimpl-classes using each other

I have three classes that I expose to the user via pimpl. The Model is a data container that can be read from and written to a file. The Manipulator is an object that can load a Model, perform changes and return it as a new Model. The Consumer loads…
Johannes
  • 3,300
  • 2
  • 20
  • 35
1
vote
1 answer

Forward declaration outside class works, but not when nested

Suppose I have two classes A and B employing the pimpl idiom. A provides the public API, holding a pointer to B. I get a compilation error when forward-declaring B within A, but not when declaring it outside. Why would the latter not work? In both…
Adama
  • 720
  • 2
  • 5
  • 23
1
vote
1 answer

Hiding implementation of members owned by PImpl-objects

I have a class which I want to create an interface for without showing any of the implementation (not because it's closed source, but because of a lot of unnecessary headers such as OpenGL coming with it), let's call that class Foo. I know this is…
1
vote
2 answers

Using the PIMPL idiom, should the implementation always be a private member of the class?

I've seen the PIMPL idiom implemented in two different ways. One approach is to always make the implementation a private member of the class. This ensures that the implementation cannot be accessed from outside the class no matter what. For…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
1
vote
1 answer

C++ Inheritance in the Pimpl idiom

Let's say I have a class B and a class A : public B which inherits from B. I want to expose the methods of A, which call some methods in B. Now I want to expose these methods in the pimpl idiom - I'm really not sure how to do this: Do both A and B…
smörkex
  • 336
  • 3
  • 18
1
vote
2 answers

How to avoid "redefinition; different basic types" when trying to implement PIMPL idom

I am trying to use the PIMPL idiom -- hiding the implementation details of a class from the user. I also want to go one step further, by hiding the actual name of the implementation class. This should also allow me to quickly swap the implementation…
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
1
vote
1 answer

Using C++ class in Objective-C - Why use PImpl idiom?

Suppose I have a simple C++ class: // MyCPPClass.hpp class MyCPPClass { std::uint8_t m_num1 = 0; std::uint8_t m_num2 = 0; std::string GetText() const; } and I want to use this in an Objective-C class: // MyObjCClass.h @interface…
DarkMatter
  • 306
  • 2
  • 13
1
vote
1 answer

Can I wrap a QWidget subclass PIMPL style without modifying it

I have a class which I'd like to hide using PIMPL type approach. This is because it is a UI form which introduces uic generated header dependancies that I don't want other parts of code to require. So far I have renamed it PrivateClass, for…
mike
  • 1,192
  • 9
  • 32
1
vote
1 answer

Compiling and linking pimpl style class definition into a library (C++)

I have a class that I use and I tried to rewrite it so it uses the pimpl idiom as an exercise. However, I now have trouble trying to compile and link the program. I have a main folder that contains a folder called data in which the pimpl…
Slugger
  • 665
  • 1
  • 5
  • 17
1
vote
1 answer

using D_ptr implementation destructor

I tried implementing PIMPL method of using D_ptr in Qt widget. The following code is what I implemented. class GuiCentralHandler : public QWidget { Q_OBJECT public: GuiCentralHandler (QWidget *parent = 0); ~GuiCentralHandler…
Wagmare
  • 1,354
  • 1
  • 24
  • 58
1
vote
3 answers

Pimpl idiom with Qt

I am trying to build a shared library with Qt 5.6.0 and i would like to use Pimpl. I have one class to export and this class inherits from QGraphicsScene, which itself inherits from QObject : // CustomScene.h #include "customscene_global.h" //…
Fryz
  • 2,119
  • 2
  • 25
  • 45
1
vote
2 answers

invalid use of incomplete type error

This is simplified code just to show my question: main.cpp #include "one.hpp" #include int main(){ One one; std::cout << one.two->val; } one.hpp: struct Two; <- forward declare Two struct One{ One(); ~One() { delete two;} Two*…
Mateusz Wojtczak
  • 1,621
  • 1
  • 12
  • 28
1
vote
1 answer

C/C++ API design dilemma

I have been analysing the problem of API design in C++ and how to work around a big hole in the language when it comes to separating interfaces from implementations. I am a purist and strongly believe in neatly separating the public interface of a…
keebus
  • 990
  • 1
  • 8
  • 15