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

(C++) Crash while calling outer class method in constructor which should set inner class's member

I have two files: test.h #ifndef TEST_H_INCLUDED #define TEST_H_INCLUDED class A{ private: class B; B *bp; public: A(int val); void setX(int value); }; #endif // TEST_H_INCLUDED test.cpp #include "test.h" class A::B{ int x; …
user1242967
  • 1,220
  • 3
  • 18
  • 30
1
vote
4 answers

Pimpl with header containing the class

I came across an implementation which had the pimpl class as a header and included that in the pimpl implementation. Does it even make sense? Something like this: UI.h class UI { public: UI(); virtual ~UI(); // bunch of…
suma c
  • 13
  • 3
1
vote
1 answer

Unknown Method Return Type with Polymorphic Template Classes in C++

I have been struggling to figure out how to implement the following classes. Essentially what I am trying to achieve is the following: - The main class is for a Matrix - The data should be stored external to the Matrix object - The Matrix_Data…
Shmuel Levine
  • 550
  • 5
  • 18
1
vote
2 answers

Using pimpl with Templated Class and explicitly instantiated templates

How do I use pimpl for a templated class, when I explicitly instantiate the templates? All I need is an example code. What I have tried is: // MyTemplatedClass.h template< class T > class MyTemplatedClass { private: class Impl; Impl*…
abaldur
  • 29
  • 1
  • 3
1
vote
1 answer

Invalid use of incomplete type on qt private class

I want to use the d-pointer in a derived class with the help of Q_D macro. Here is my parent class: class DIGIKAM_EXPORT GraphicsDImgView : public QGraphicsView { Q_OBJECT public: class GraphicsDImgViewPrivate; protected: …
wceo
  • 934
  • 3
  • 18
  • 40
1
vote
2 answers

C++ const correctness vulerability or unintended usage?

I am missing something or const-correctness doesn't work quite as intended with pointers (or perhaps smart pointers since that is what I've tested?). Anyway, here is what I observed with trying out a variant of the PIMPL idiom. I have the following…
squashed.bugaboo
  • 1,338
  • 2
  • 20
  • 36
1
vote
3 answers

Problems with compiling a pimpl idiom code

I've been trying to have a go at a 'pimpl' idiom but I just can't get the darned thing to compile. On Linux Mint with g++ v. 4.6.3 I get the following error: $ g++ main.cc /tmp/ccXQ9X9O.o: In function `main': main.cc:(.text+0xd7): undefined…
Nobilis
  • 7,310
  • 1
  • 33
  • 67
1
vote
3 answers

PIMPL problem: How to have multiple interfaces to the impl w/o code duplication

I have this pimpl design where the implementation classes are polymorphic but the interfaces are supposed to just contain a pointer, making them polymorphic somewhat defeats the purpose of the design. So I create my Impl and Intf base classes to…
jmucchiello
  • 18,754
  • 7
  • 41
  • 61
1
vote
5 answers

pimpl idiom and template class friend

I'm trying to use the pimpl idiom to hide some grungy template code, but I can't give derived classes of the body class friend access to the handle class. I get an error C2248 from MSVC 9 sp1. Here's some code to duplicate the error: // //…
Queueless
  • 113
  • 1
  • 7
1
vote
2 answers

Templated classes with pimpl idiom incorrect

As described in the MSDN library here I wanted to experiment a bit with the pimpl idiom. Right now I have a Foo.hpp with template class Foo { public: typedef std::shared_ptr> Ptr; Foo(); private: class Impl; …
Christian Ivicevic
  • 10,071
  • 7
  • 39
  • 74
1
vote
1 answer

The compiler shoots itself in the foot when trying to optimise/inline my trivially looking but non-trivial dtor, what am I doing wrong?

I have this shared pimpl*. It forward declares the implementation object and has a custom-implemented shared pointer object to implement the pimpl idiom (again, with sharing semantics). Condensed, it looks like this: Foo.h #include…
bitmask
  • 32,434
  • 14
  • 99
  • 159
0
votes
1 answer

How to call copy constructor of a caller class from pimpl class?

I just need to know if I want to call my copyconstuctor from pImpl class, how will I do it? For example: CImpl::SomeFunc() { //cloning the caller class instance caller = new Caller(*this)// I cant do this since its a pImpl class } How can i…
Yogi
  • 1,035
  • 2
  • 13
  • 39
0
votes
3 answers

Avoid leaking out external types in a C++ class

I have a class defined in a header like so (abbreviated): class CairoRenderer { public: CairoRenderer(); ~CairoRenderer(); ... protected: cairo_t* m_context; cairo_surface_t* m_surface; }; cairo_t and cairo_surface_t are types…
jumpinjackie
  • 2,387
  • 4
  • 22
  • 26
0
votes
0 answers

declaration and default initialization of member-variables in PIMPL interfaces?

We are developping an embedded project, where we use C++14 and PIMPL for the OS-Abstraction. So we have Task-PIMPL-Interface which gets implemented in the referenced operating-system-SDK. Currently a small additional effort must be done because this…
NetoBF
  • 127
  • 6
0
votes
2 answers

Linker error while implementing pimpl idiom

Edited to provider a little more clarity. Apologies for confusing everyone. This is under Windows. I have a static library that implements a class using the pimpl idiom. The pimpl header is not only used by the consuming code but it also links…
ForeverLearning
  • 6,352
  • 3
  • 27
  • 33