Boost logo

Boost Users :

From: Pavel Antokolsky aka Zigmar (zigmar_at_[hidden])
Date: 2005-05-19 05:15:07


> When working with Qt, i suggest to write the Pimpl idiom like this,
> since the generated code from moc needs access to your FooPrivate,
> in cases.
>
> struct FooPrivate;
> class Foo
> {
> private:
> FooPrivate* pimpl_;
> };
I would not suggest using such implementation. It has few problems:
1. Global namespace pollution. That can be easily fixed, by making
FooPrivate innter class of Foo.
2. High danger of unexpected behaviour if implementation of assignment
and copy contrsuctor is omitted or incorrect. If you forget to
implement copy operator and then copy such object - results will be
devastating - both objects will share pointer on same state.

For same reason, I suggest using scoped_ptr over shared_ptr. If
you'll try to copy object with no assignment operator and
scoped_ptr-based pimpl - you will get a nice compile error. Using
shared_ptr it will work fine, but, probably, it is not what was
intended to do (until, of course, the intention is implicitly shared
state).

-- 
Best regards,
Zigmar

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net