|
Boost Users : |
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-05-16 18:13:11
Marshall Clow wrote:
> At 2:37 PM -0500 5/16/08, Nevin \":-]\" Liber wrote:
>> 2008/5/16 Kevin Martin <kev82_at_[hidden]>:
>>> My problem is module interfaces. I have a module (set of classes) that
>>> solve a problem, and they require data. They don't care whether that
>>> data is on the heap, the stack, or the moon. They just want a pointer
>>> to it. In this case it is arguable that the pointers passed shouldn't
>>> be smart pointers at all, just normal pointers.
>> My general rule of thumb is that if mucking with ownership or
>> lifetime, pass a smart pointer. If not, pass a raw pointer.
>
> A brief addenda (from my rules of thumb) for non-ownership cases:
> * If the parameter is optional, pass a raw pointer, and the
> callee should
> expect a NULL pointer.
> * If the parameter is not optional, pass a reference.
I don't like this idea. You are creating a dependency on the fact that
the called function will NOT keep a copy - or you are insisting that the
object in question implement the shared_from_this model. If you later
decide that it would be prudent for the function or object in question
to create or pass a kept copy of this object then you'll have to change
the signature of the function and then each and every call to it (to
remove get() calls).
So while I can see some possible enforcement advantages to this in the
rare case, I certainly wouldn't use it as a general rule of thumb. I
can't count the number of times someone has assumed that nobody would
need a copy of something and then requirements changing dictated that
someone did.
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