Re: C++ Primer 4th edition Reference Counting Smart Pointers
On Jun 29, 10:19 pm, Hendrik Schober <spamt...@gmx.de> wrote:
CplusplusNewbie wrote:
[...]
However, it isn't very sophisticated (perhaps rightly so,
since it is, after all, intended as a book for novices), and
the C++ Primer code does nothing to address the issue of
cycles of pointers. (Boost handles this by the weak_ptr
class template.) Presumably, Scott Meyers did address this
issue, and maybe others.
No, he didn't. I think you're referring to Item 28 of
"More Effective C++" and I've just skimmed over it. I
found nothing of cycles.
I don't know if he mentionned them, but his reference pointers
don't handle cycles.
So, a safe bet is that the reason Scott Meyers (according to
his own account) struggled a bit on his implementation,
whereas C++ Primer handles the matter simply, is that the
Meyers approach was much more ambitious in its scope.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html#Im...
I wonder if he isn't letting himself be led astray by Boost.
boost::shared_ptr attempts to do a lot more than Scott's
reference counted pointer (which is perfectly adequate, and in
fact preferable to boost::shared_ptr for most uses).
Of the three "difficulties" he explicitly mentions, the first
two concern possible exceptions from functions which in a simple
reference counted pointer are automatically no throw (since they
only involve incrementing and decrementing integers and
assigning pointers). As for the third, it's really just a
special case of a more general problem, related to self
assignment, and isn't present if you use the swap idiom for
assignment, or any of the other means for handling self
assignment (i.e. anything which avoids premature destruction of
the right hand side of the assignment).
With regards to the last example, however: reference counted
pointers are not a panacea. As a general rule, it's probably
best to avoid using reference counted pointers on objects which
themselves contain reference counted pointers.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34