Boost logo

Boost :

Subject: Re: [boost] Pimpl Again?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2016-05-31 19:47:25


On 1/06/2016 11:05, rstewart wrote:
>>> The upside is not writing some calls one way and others the other way on
>>> the same object, and having to remember which is which.
>>>
>> So, don't use the dot syntax. :)
>
> Not all functions can be non-members.

Operators can be; so the only types of functions I'm aware of that must
be members are the constructors (regular, copy, and move).

But constructors simply don't exist with this design, since you use
factory methods in place of regular constructors, and copy/move
construction is not possible with an incomplete type (which is all that
external code will ever see).

Moving is not really an issue -- you can move the shared_ptr instead.
The same applies to shallow copying (copying the pointer rather than the
underlying object).

If you want to be able to deep copy the object then this requires an
explicit API (typically a "shared_ptr<T> clone(T*)" factory method).
This is the reverse of traditional C++ objects which are copyable by
default -- these are copyable only where explicitly permitted. I'm sure
there will be people who argue that this is a good thing and those that
argue that it isn't. :)

I've used a similar coding style when writing object-oriented C code,
although I admit it would feel weirder doing it in C++ (although this
has some natural benefits, such as overloading).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk