C++ pimpl

so so at so.so
Fri Feb 10 11:23:59 PST 2012


On Monday, 23 January 2012 at 17:09:30 UTC, so wrote:
> On Mon, 23 Jan 2012 18:09:58 +0200, Robert Caravani 
> <jfanatiker at gmx.at> wrote:
>
>> Thanks for the links, it was a good read.
>
> I think it is the best answer to the problem.
>
>> What's the destructor limitation?
>
> struct S {
>  static S* make(); // constructor
>  static void purge(S*); // destructor - you have to provide 
> this as well
> }
>
> Limitation is that just like constructor, we also lose the 
> destructor.
> Which means we can't use "delete", and the tools designed for 
> it.
> As a result we don't have something new. It is exactly like C 
> impl. hiding.
>
> struct S;
> S* make();
> void purge(S*);

I think i finally got a solution.
(Sorry for C++ code)

--- 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;
}


More information about the Digitalmars-d mailing list