C++ pimpl

Robert Caravani jfanatiker at gmx.at
Sun Feb 19 09:26:26 PST 2012


On Friday 10 February 2012 20:23:59 so wrote:
> I think i finally got a solution.
> (Sorry for C++ code)
Never mind, it is my mother tongue.
> 
> --- pimpl.hpp
> template<typename T>
> struct pimpl : noncopyable
> {
>   virtual ~pimpl() {}
> };
> 
> --- lib.hpp
> struct lib : public pimpl<lib>
> {
>   void fun(...);
>   ...
>   static lib* make();
> };
> 
> --- lib.cpp
> struct lib_impl : lib
> {
>   ... // data
> };
> 
> void lib::fun(...)
> {
>   auto& r = *static_cast<lib_impl*>(this);
>   ...
> }
> 
> lib* lib::make()
> {
>   return new lib_impl;
> }

Well it is not bad, but how do you support derived classes with this scheme? 
Derived classes would have to derive from the implementation, so it would have 
to be public and compiled code using the derived class would break on changes 
of the private fields. Am I misinterpreting something?

What's the purpose of the pimpl template?

What do you think about my proposal? Is it sane and feasible at all?

Best regards,

Robert


More information about the Digitalmars-d mailing list