Re: C++ Primer 4th edition Reference Counting Smart Pointers
On Jun 30, 11:24 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
James Kanze <james.ka...@gmail.com> kirjutas:
As a general rule, it's probably best to avoid using
reference counted pointers on objects which themselves
contain reference counted pointers.
Can you give some rationale for this claim? Is it just a dead
sure way to avoid cycles? I'm asking because at least 90% of
smartpointers in our applications reside inside of other
smartpointed objects.
Obviously, it is a 100% sure means of avoiding cycles.
More generally, however: what sort of object would be managed by
a reference counted pointer, and would also contain one? There
are probably exceptions, but the only types of objects I can
reasonably see containing reference counted pointers are entity
objects, and you don't want reference counted pointers to entity
objects, since entity objects normally have deterministic
lifespans. If I look at my own code, almost all of the objects
I have which are managed by reference counted pointers are
agents of some sort or another---small polymorphic objects with
no state of their own, which are created and used to do just one
special thing. Any pointers they have are for navigation, and
so are either raw pointers, or if there is some chance that the
lifetime of the object referred to ends before the agent is
finished, some sort of "observer" pointer, which will
automatically be null'ed in the destructor of the pointed to
object.
--
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