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.

281 questions
19
votes
6 answers

Could C++ have not obviated the pimpl idiom?

As I understand, the pimpl idiom is exists only because C++ forces you to place all the private class members in the header. If the header were to contain only the public interface, theoretically, any change in class implementation would not have…
Frederick The Fool
  • 31,355
  • 20
  • 78
  • 112
18
votes
5 answers

Pimpl idiom with inheritance

I want to use pimpl idiom with inheritance. Here is the base public class and its implementation class: class A { public: A(){pAImpl = new AImpl;}; void foo(){pAImpl->foo();}; private: AImpl* pAImpl; }; class AImpl { …
Igor Oks
  • 24,930
  • 25
  • 85
  • 112
17
votes
3 answers

pimpl for a templated class

I want to use the pimpl idiom to avoid having users of my library need our external dependencies (like boost, etc) however when my class is templated that seems to be impossible because the methods must be in the header. Is there something I can do…
David
  • 25,830
  • 16
  • 80
  • 130
17
votes
1 answer

What are the rules for noexcept on default defined move constructors?

Especially in connection with std::vector it is important that types are noexcept movable when possible. So when declaring a move constructor = default like in struct Object1 { Object1(Object1 &&other) =…
mfuchs
  • 1,950
  • 9
  • 19
15
votes
3 answers

Is the pimpl idiom used in c#?

I come from a C# background and have recently started learning C++. One of the things I've encountered is the pimpl idiom. I've done C# development for some large firms, but never came across it. Maybe this is wrong, but my understanding is that…
Legion
  • 3,295
  • 7
  • 39
  • 78
14
votes
1 answer

Heap-free pimpl. Incorrect or superstition?

Edit: This question dates from before C++17. These days std::launder or equivalent should be added to the line noise. I don't have time to update the code to match right now. I am aspiring to separate interface from implementation. This is primarily…
Jon Chesterfield
  • 2,131
  • 1
  • 18
  • 25
12
votes
3 answers

delegating into private parts

Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar; Bar* p; public: Bar* operator->() const { return p; } }; struct Foo::Bar { void baz() { std::cout << "inside baz\n"; …
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
12
votes
3 answers

Automate pimpl'ing of C++ classes -- is there an easy way?

Pimpl's are a source of boilerplate in a lot of C++ code. They seem like the kind of thing that a combination of macros, templates, and maybe a little external tool help could solve, but I'm not sure what the easiest way would be. I've seen…
Joseph Garvin
  • 18,725
  • 17
  • 83
  • 150
11
votes
3 answers

What are the pros and cons of using d-pointers?

d-pointers are heavily used in Qt, they are an implementation of pimpl idiom. I know advantages and disadvantages of pimpl idiom. But I have missed the advantages of d-pointers implementation. Here and here are the samples of d-pointers. Isn't it…
Dmitriy
  • 4,981
  • 7
  • 42
  • 53
11
votes
4 answers

Hide implementation by using a pointer (Pimpl idiom)

Is it somehow possible, to accomplish the following: x.hpp - this file is included by many other classes class x_impl; //forward declare class x { public: //methods... private: x_impl* impl_; }; x.cpp - the…
emesx
  • 12,287
  • 7
  • 53
  • 91
11
votes
1 answer

Java programming idiom : Private implementation class

I found this construct in some code. Is there any benefit to have a private static class implement A? This reminded me of the Pimpl idiom in C++. Is there any benefit to using the Pimpl idiom in Java? public abstract class A { public void…
Chip
  • 3,016
  • 20
  • 27
10
votes
2 answers

Implementing pImpl with minimal amount of code

What kind of tricks can be used to minimize the workload of implementing pImpl classes? Header: class Foo { struct Impl; boost::scoped_ptr self; public: Foo(int arg); ~Foo(); // Public member functions go…
Tronic
  • 9,852
  • 2
  • 38
  • 50
10
votes
1 answer

Pimpl with smart ptr - Why constructor/destructor needed

Lets consider following example (using c++11) A.hpp: #include class A { public: //A(); //~A(); private: struct AImpl; std::unique_ptr pImpl; }; main.cpp: #include "A.hpp" int main() { A a; } Using default…
meddle0106
  • 1,232
  • 1
  • 11
  • 20
10
votes
1 answer

Viewing a pimpl from DLL in debugger

I am using the pimpl idiom to hide the implementation details of an interface so that I can have some measure of ABI protection. I'm not that well versed on the ins and outs of MS...using Linux for most of my development career. I'm unable to view…
Edward Strange
  • 38,861
  • 7
  • 65
  • 123
9
votes
7 answers

How does the pimpl idiom reduce dependencies?

Consider the following: PImpl.hpp class Impl; class PImpl { Impl* pimpl; PImpl() : pimpl(new Impl) { } ~PImpl() { delete pimpl; } void DoSomething(); }; PImpl.cpp #include "PImpl.hpp" #include "Impl.hpp" void PImpl::DoSomething()…
Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
1
2
3
18 19