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

Having a typedef in the source file and a member variable in header

I have a class structure where I have some member variables declared like this: /* HEADER */ #ifndef SRC_HEADER_H #define SRC_HEADER_H class Service { private: typedef boost::multi_index_container<...> Checkpoints; Checkpoints checkpoints_…
Vaibhav
  • 346
  • 2
  • 12
1
vote
2 answers

Passing the partially constructed object in pimpl

I have a class setup that I have converted to use pimpl, something like this: (outline only, I'm aware this doesn't compile) struct GAME { unique_ptr _impl; explicit GAME() : _impl( new GAME_IMPL( *this ) ); IMAGES…
c z
  • 7,726
  • 3
  • 46
  • 59
1
vote
2 answers

Typecasting structs to hide implementation vs pimpl-idiom

I know about the pimpl-idiom which in C would look something like this: // foobar.h struct FooBar { char *someString; struct FooBarImpl *pImpl; }; // foobar.c struct FooBarImpl { char *hiddenString; }; However with typecasting I can get…
user368716
1
vote
1 answer

How to give access to public members with Pimpl?

pimpl.h #include class MyClassImpl; class MyClass { void Foo(); struct MyStruct { int a; int b; } variable_struct; private: std::unique_ptr m_pImpl; }; pimpl.cpp class MyClassImpl { public: …
Alexis
  • 2,136
  • 2
  • 19
  • 47
1
vote
0 answers

PImpl with C++: Why does the code not work?

I've solved the problem by putting #include "stdafx.h" (this statement is missed in the original question, sorry for that) BEFORE #include "PImplTest.h" instead of AFTER it. But I'm still confused why it cannot stay after it. // stdafx.h #include…
Irene
  • 11
  • 3
1
vote
0 answers

Hiding library dependencies using Pimpl doesn't seem to do anything

I am using PIMPL to hide implementation details of my library from the end user. Also want to capture all 3rd party dependencies of the library within itself; so that I would not need the end user to link them to use my library. The Interface ////…
Roy2511
  • 938
  • 1
  • 5
  • 22
1
vote
2 answers

Creating library using pimpl-idiom

I am trying to define interfaces for a library which will be using pimpl-idiom. Following is a typical interface class which I define. struct A { public: void func1(); void func2(); virtual void notif1(); virtual void notif2(); private: …
user761867
  • 113
  • 1
  • 5
1
vote
0 answers

Removing pimpl from header file

I have currently implemented a library using the pImpl idiom as such (just an example); // library.h class lib_public_type { class impl; std::unique_ptr impl__; public: void libTypeFunction(); } // library.cpp #include…
Thizzer
  • 16,153
  • 28
  • 98
  • 139
1
vote
0 answers

C++ library hiding member data structures in pimpl

I'm trying to create a library which exposes one class to the outside with a pimpl idiom. This implementation works, meaning I can add functionality to the "idcomimpl" class without breaking ABI. But how do I add the data structure in this whole…
xyfix
  • 61
  • 1
  • 7
1
vote
1 answer

Implementing pimpl-friendly unique_ptr

It is widely known that std::unique_ptr may not be conveniently used to implement pimpl idiom: one may not default destructor and move operator right in the header file (e.g., std::unique_ptr with an incomplete type won't compile). Some people…
Nikita Petrenko
  • 1,068
  • 1
  • 7
  • 10
1
vote
1 answer

Hiding Implementation Details in C++

I would like to hide implementation specific details from the interfaces defined in the header so the code is maintainable and quicker to compile when making updates (although I don't have statistics for the latter). However, I cant use dynamic…
electronpygmy
  • 51
  • 1
  • 5
1
vote
2 answers

reduce size of object (wasted) in Multi virtual inheritance

After profiling, I found that a large portion of memory of my program are wasted by multi-virtual-inheritance. This is MCVE to demostrate the problem ( http://coliru.stacked-crooked.com/a/0509965bea19f8d9 ) #include class Base{ …
1
vote
1 answer

Using the PIMPL idiom with member function templates (without knowing all possible data types up-front)

I am trying to implement a generic interface using the PIMPL idiom that requires some template member functions to provide functionality for ANY type. The issue is I can't find any way to support using template functions with the PIMPL idiom without…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
1
vote
2 answers

pimpl idiom struct memory leak

We are using the pimpl idiom in our classes. The pimpl struct is declared in the class which contains the pimpl pointer like so: struct MyClassImpl; friend struct MyClassImpl; boost::scoped_ptr m_Impl; The implementation for the pimpl…
Bizmarck
  • 2,663
  • 2
  • 33
  • 48
1
vote
0 answers

How can I reuse the same impl destructor in a class derived from a PIMPL base class?

I'm trying to create a class that inherits from a base class that uses the PIMPL idiom. I have a base class base.h: #include class Base { public: class Impl; Base(std::unique_ptr impl); virtual ~Base(); private: …
srujzs
  • 340
  • 3
  • 14