On 21.10.2013 15:45, mathieu wrote:
> I have been reading "The Joy of Pimpls " [1], but this seems over-complicated
> IMHO. It requires a "back-pointer" just to split object in halves.
>
> I am thinking of providing something similar (at least in my scenario), where I
> can hide the implementation completely.
>
> I am thinking in something like this:
>
> $ cat demo.cxx
> // public stuff:
> #include <iosfwd>
Good practice. :-)
Every variant has its own advantages and drawbacks.
Wit the common PIMPL idiom client code can inherit the implementation of
the exposed class, i.e. client code can inherit a concrete, instantiable
class. This is lost with the abstract class idea (above). But one avoids
some indirection and dynamic allocation.
Not sure if Herb mentions this, but the main purpose of a PIMPL is to
encapsulate use of dirty or large or platform-specific (whatever)
headers. If that's not a problem, then PIMPL is usually not needed. A
main cost of PIMPL is that it requires separate compilation.
> How would one mark the class 'visible' an interface-only (it should
> not be derived in user applications).
For C++11, <url:
http://en.cppreference.com/w/cpp/language/final>, using
the keyword "final".
For C++03, <url:
http://www.parashift.com/c++-faq/final-classes.html>,
using a virtual base class with limited accessibility (because a virtual
base class must be constructed in the most derived class).
But consider whether there is any concrete advantage in doing that: it's
often a good idea to make the intended usage natural and easy and the
hazardous usage less easy, but (well OK it's famous last words) what
could possibly go wrong?
Cheers & hth.,
- Alf