pimpl-idiom

Why should the "PIMPL" idiom be used?

Backgrounder: The PIMPL Idiom is a technique for implementation hiding in which a public class wraps a structure or class that cannot be seen outside the library the public class is part of. This hides internal implementation details and data from the user of the library. When implementing the this idiom why would you place the public...

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 necessitated a recompile for the rest of the program. What I want to know is why C++ is no...

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 get Linker warning 4248: warning LNK4248: unresolved typeref token (0100000E) for 'MyN...

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 { public: void foo(){/*do something*/}; }; And I want to be able to create the der...

How to get debug information for an abstract(?) pimpl in C++?

I have a wrapper class that delegates its work to a pimpl, and the pimpl is a pointer to a baseclass/interface with no data that is specialized in several different ways. Like this: class Base { void doStuff=0; }; class Derived { int x,y; void doStuff() { x = (x+y*2)*x; //whatever } }; class Wrapper { Bas...

Pimpl idiom vs Pure virtual class interface

I was wondering what would make a programmer to choose either Pimpl idiom or pure virtual class and inheritance. I understand that pimpl idiom comes with one explicit extra indirection for each public method and the object creation overhead. The Pure virtual class in the other hand comes with implicit indirection(vtable) for the inheri...

The Pimpl Idiom in practice

There have been a few questions on SO about the pimpl idiom, but I'm more curious about how often it is leveraged in practice. I understand there are some trade-offs between performance and encapsulation, plus some debugging annoyances due to the extra redirection. With that, is this something that should be adopted on a per-class, or ...

What patterns do you use to decouple interfaces and implementation in C++?

One problem in large C++ projects can be build times. There is some class high up in your dependency tree which you would need to work on, but usually you avoid doing so because every build takes a very long time. You don't necessarily want to change its public interface, but maybe you want to change its private members (add a cache-vari...

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: // // interface.hpp // namespace internal{ template<class T> class specific_body; } class int...

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 templates that help do some of the lifting but not much -- you still end up needing to write forwa...

Is it possible to wrap boost sockets with Pimpl?

Hi, in a project we want to wrap the Boost Asio socket in a way, that the using class or the wrapping .h does not have to include the boost headers. We usually use pointers and forward declarations for wrapped classes. Foward declaration: namespace boost { namespace asio { namespace ip { class udp; } } } And...

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 provide reference counting. And then the user can create their implementations. An example: ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such a noob. ...

Workarounds for the forward-declared class enumeration problem?

I am maintaining a large code base and am using a combination of forward declarations and the pImpl idiom to keep compile times down and reduce dependencies (and it works really well,) The problem I have is with classes that contain public enumerations. These enumerations cannot be forward declared so I am left with no option but to in...

STL-friendly pImpl class?

I am maintaining a project that can take a considerable time to build so am trying to reduce dependencies where possible. Some of the classes could make use if the pImpl idiom and I want to make sure I do this correctly and that the classes will play nicely with the STL (especially containers.) Here is a sample of what I plan to do - d...

Private members in pimpl class?

Is there any reason for the implementation class as used in the pimpl idiom to have any private members at all? The only reason I can really think of is to protect yourself from yourself -- i.e. the private members serve to enforce some kind of contract between the class and the user, and in this case the class and the user are rather in...

Opaque object for template in another namespace...

Hi, I know how to do an opaque object in C++ as following: // my_class.hpp class opaque_object; class my_class { my_class(); ~my_class(); opaque_object *m_opaque_object; }; // my_class.cpp #include <my_class.hpp> class opaque_object { // ... }; my_class::my_class() { m_opaque_object = new opaque_object(); } my_class::~...

How to use Loki's Pimpl implementation?

Link to source code of Loki Pimpl header. I am not able to find any documentation on how to use the same, can any one explain how to use. And what does the following function in the header do. PimplOwner ImplOf PimplOf RimplOf ...

Remove dependancy constants from enum definition

I am trying to safely remove a dependency from my project by using opaque structures and forward declarations but like most I am still stuck on my enums. The header file dependency I am trying to remove from my header files has defined constants that I want to set my enumeration's values to. Something like this // depends header #defi...

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<Impl> self; public: Foo(int arg); ~Foo(); // Public member functions go here }; Implementation: struct Foo::Impl { Impl(int arg): something(arg) {} // All data members...