|
Boost : |
From: Jani Kajala (jani_at_[hidden])
Date: 2002-01-16 09:15:15
> I have a function that takes 3 pointers as arguments. Now they've all been
> changed to shared_ptrs. Show me how that function can be called (I know how
> to do it, but it isn't pretty, even with bind()).
I think these and other problems would be avoided if the smart pointer would
only manipulate reference count which is stored in a common base class:
1) You'd be able to construct smart pointers from a regular pointer, even if
there were other smart pointers to the same object elsewhere.
2) You could always pass a regular pointer (by implicit cast from smart ptr) to
a function. It would also mean much less overhead as reference counts would only
be changed when someone stores the pointer for later use.
3) You would not be forced to always use smart pointers with certain function,
but you could have the same functions work on non-RCd and RCd objects without
using templates.
4) You can always get reference counted version from some primitive or external
type (like iostream classes) by adding extra level of indirection.
5) The biggest reason for not having the reference count in the pointer is that
I do not find it practical in real world situations. I've tried to use such
approach in the past I wouldn't try it again. Reference count in the base class
is much more flexible approach what it comes to mixing smart pointer and normal
pointer usage. (and that kind of mixing is always needed because of the
fundamental reason that smart pointers are not real pointers even though they
look like one)
http://www.helsinki.fi/~kajala/code/PtrExample.cpp
is an example of a reference counted user class, int and ifstream using approach
above. (all classes are declared and defined in the same cpp file for
compactness)
Regards,
Jani Kajala
PS. Sorry if you received this mail twice. I already send this morning but it
seems that it didn't go through.
----- Original Message -----
From: "David Abrahams" <david.abrahams_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, January 16, 2002 2:55 AM
Subject: Re: [boost] Loki SmartPtr questions
>
> ----- Original Message -----
> From: "Gustavo Guerra" <gustavobt_at_[hidden]>
>
> > Why don't we drop get and release and instead put something like this:
> >
> > template<T>
> > class shared_ptr
> > {
> > private:
> > T* ptr;
> > public:
> > template<class F>
> > void call_legacy_function(F callback)
> > {
> > callback(ptr);
> > }
> > };
> >
> > Of course the callback could still do something bad with the pointer, but
> > it's more localized.
>
> I have a function that takes 3 pointers as arguments. Now they've all been
> changed to shared_ptrs. Show me how that function can be called (I know how
> to do it, but it isn't pretty, even with bind()).
>
> -Dave
>
>
> Info: http://www.boost.org Send unsubscribe requests to:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk