
Marshall Clow wrote:
At 2:37 PM -0500 5/16/08, Nevin \":-]\" Liber wrote:
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
2008/5/16 Kevin Martin
: 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.