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
Daniel Oberhoff wrote:
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(); };
How can the user access Variable::ref if he already doesn't have a reference to Variable to start with?
On 6/28/07, Daniel Oberhoff
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(...));
Take a look at shared_from_this(), I believe that is what you want. http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html -- --------------- Joe Angell Boulder Labs, Inc. 7105 LaVista Pl Suite 200 Niwot, CO 80503 (303) 817-8252
participants (3)
-
Daniel Oberhoff
-
Joe Angell
-
Peter Dimov