Boost logo

Boost Users :

From: Daniel Oberhoff (daniel_at_[hidden])
Date: 2007-06-28 08:33:45


Hi all,

I am using shared_ptr to ease implementation of a matlab file
wrapper. Esepcially the fact that it is in tr1 and thus available on
many platforms already pushed that decision. Now I was wondering about
one thing:

Is there a devent way, using shared_ptr, to hand out multiple
references? What I mean is something like this:

class Variable {
       shared_ptr<Variable> ref();
};

The problem with shared_ptr is, that, or so it seems, you can only
create one directly from the object like this:

shared_ptr<Variable> p(new Variable(...));

and all further references have to be created as copies of this (or
copies of copies...). So I have to keep one of those around to make new
ones. But I cannot keep one inside the class itself or else it will
never be deleted (because it would reference itself). The same goes for
weak_ptr, since there must be a live shared_ptr around for the object
not to be deleted.

The only way I can think of is implementing my own refcount and hooking
into my own release method. THe problem with this is that I cannot,
acoording to the docs, hook up the retain method, so each shared_ptr
"cloud" (i.e. set of shared_ptrs that have been created from a common
shared_ptr) is hooked up to one ref_count, so they might invalidate when
the object stays alive, making any attached weak_ptrs useless.

The other is using intrusive_ptr, but that a) is not in tr1 and b) does
not have a weak ptr.

I was also wondering why intrusive_ptr is not wrapped inside shared_ptr,
i.e. why I cannot hook up both retain (increase refcount) and release
into shared_ptr, but I suppose that is for the developers list.

One other way I could think of would be that I keep a reference to the
global ref_count of all the shared_ptrs, and creating new shared_ptrs on
top of that, thus binding all shared_ptrs together, but that part does
not seem to be exposed in the api.

Any Ideas?

Daniel Oberhoff

ps: should it be possible to post to this list via gmane? I would
like that much better, so this won't clog my inbox. But trying to
post I keep getting error 441 in gnus


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