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

get return type from a function of a class that was forward-ed declaraion

Is it possible to get return type UglyIterator<> of a function of a class B::f which is forward declaration? Example MyArray is a class that acts like std::vector. Its begin() and end() function return a ugly type. template class MyArray{ …
javaLover
  • 6,347
  • 2
  • 22
  • 67
2
votes
2 answers

Pimpl not working

This is a very noobish mistake, but I dont know whats happening here. There are loads of pimpl examples but I dont understand why this isn't working (this was one of the examples more or less but I dont see the difference). I have a very simple…
user245019
2
votes
1 answer

pimpl idiom using anonymous-namespace structure: is this safe?

A teammate of mine regularly uses a variation on pimpl, which he does like this: Foo.h: namespace { struct Impl; } class Foo { public: Foo(); ~Foo(); void Bar(int n); /* ... */ private: std::unique_ptr _impl; }; What's happening…
Ziv
  • 2,369
  • 3
  • 24
  • 40
2
votes
4 answers

Why am I getting garbage values when I print the value?

I'm trying to learn about the PIMPL idiom, and c++ in general. I have a class with a PIMPL style interface, that sets an int value to 7. But, I'm getting a garbage value when I print it, and I don't understand why. Code Test.cpp #include…
Rethipher
  • 336
  • 1
  • 14
2
votes
2 answers

PIMPL idiom VS forward declaration

I have read a bit about the PIMPL idiom and was wondering - is it any different to forward declaring the dependent type(s)? If so: When will I prefer using that over a forward declaration? Do these two versions differ in their compilation time? Is…
Mr. Anderson
  • 1,609
  • 1
  • 13
  • 24
2
votes
3 answers

pimpl with inheritance using smart pointer

Please see my implementation of PIMPL with inheritance. In derived class, DerivedImpl inherits from BaseImpl. Question: Should the pointer to Impl only defined in base class like the following code? If so, every time I need to use the base pointer,…
Yoh
  • 199
  • 1
  • 13
2
votes
2 answers

MSVC++ Linker warning when using PIMPL idiom in C++/CLI

I am writing a .NET assembly using C++/CLI (version 9.0), and I would like to use the PIMPL idiom to avoid putting unnecessary stuff in my public header. Unfortunately, when I try to forward declare a class, and then use a tracking handle to it, I…
Brian Stewart
  • 9,157
  • 11
  • 54
  • 66
2
votes
2 answers

Opaque Pointer (pimpl) and signals and slots

I am getting more and more into the Pimpl idiom (private opaque pointer to real class implementation). But I still have an issue which bothers me. How does this idiom\design pattern deal with signals in the public class (like boost or qt…
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
2
votes
1 answer

Pimpl with std::unique_ptr in a derived class

I am failing to understand the following scenario. It is about using the pimpl idiom based on the std::unique_ptr in a derived class. Given a simple class hierarchy declared as follows: class Foo { public: virtual ~Foo(); //... }; struct…
Marko
  • 21
  • 5
2
votes
1 answer

Is there a proposal to extend the C++ language so as to obviate pimpl?

Sometimes, you want to provide a class declaration, which is not merely an opaque forward declaration but has the public functionality exposed - yet you don't want to commit to your private, or implementation-specific, fields and methods. One…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
2
votes
3 answers

C++: Forward declaration of a class with a deleter for a unique pointer that can be repeated

In C++, it is possible to use forward declaration to allow pointers to an incomplete type. class A; A *p; However, in some cases I want to declare a unique pointer instead, so I use class A; class A_Deleter { void operator()(A*); } unique_ptr
tohava
  • 5,344
  • 1
  • 25
  • 47
2
votes
1 answer

PIMPL: Exporting classes with single STL member (std::unique_ptr)

Suppose I have a class X with private implementation Ximpl: //Proj.h #ifdef PROJ_EXPORTS #define PROJ_API __declspec(dllexport) #else #define PROJ_API __declspec(dllimport) #endif //X.h class PROJ_API X { void foo(); //.. private: class…
Oleg Shirokikh
  • 3,447
  • 4
  • 33
  • 61
2
votes
1 answer

Difference between global vs private forward declarations in pimpl for the end user of the pimpl

Consider the following two ways of implementing the pimpl idiom: // file g_visible.h //global forward declarations class HiddenStuff_A; class HiddenStuff_B; class g_visible { public: // API goodies for the end-user private: …
user1823664
  • 1,071
  • 9
  • 16
2
votes
2 answers

pimpl desgin pattern , member functions need to be put in private class

to ensure the ABI with the pimpl pattern, is that true that we only need to put all the data members to the "Private class" ? I see in some introduction about pimpl, they also make all the functions implementations in the "Private class" and define…
user3450805
  • 661
  • 6
  • 16
2
votes
2 answers

Would this be a valid way to implement pimpl that supports inheritance?

#include #include #include class IBase { public: IBase() = default; virtual ~IBase() = default; virtual void f1() = 0; }; class IDerived { public: IDerived() = default; virtual ~IDerived() =…
NFRCR
  • 5,304
  • 6
  • 32
  • 37