|
Boost : |
From: Larry Evans (jcampbell3_at_[hidden])
Date: 2002-04-05 11:42:59
Christian Kreibich wrote:
> Hi all,
>
> my question is related to your smart pointer package which I've been
> using for a long time now, although the core of my problem is just one
> of circular dependencies, and I'm kind of embarrassed because the answer
> must be pretty obvious.
>
> I usually use your smart pointers like this:
>
> class A
> {
> public:
> typedef boost::shared_ptr<A> ptr;
> // ...
> };
>
> so that afterwards I can conveniently access the smart pointers as
>
> A::ptr aptr(new A(...));
>
> My problem now appears when I have two classes that use each other's
> smart pointers, for example as in here:
>
> class A
> {
> public:
> typedef boost::shared_ptr<A> ptr;
> virtual B::ptr createB();
> };
>
> class B
> {
> public:
> typedef boost::shared_ptr<B> ptr;
> virtual void setA(A::ptr aptr);
> private:
> A::ptr _aptr;
> };
>
> You get the idea. Is there a way to keep using my typedef style within
> the classes and resolve the circular dependencies? On a more general
> note, what is the suggested idiom for handling this situation?
>
> Any help/pointers much appreciated.
Maybe this is nitpicking, but in the above, I see a B instance pointing to an A instance, but I don't see
how an A instance points to a B. I only see a virtual A function creating a B pointer.
Also, B::ptr is not declared when used in A's declaration.
Now if you've actually got A <--> B, then couldn't you make one typedef a weak_ptr and the other
a "strong" pointer, i.e. a shared_ptr<X>?
If not, then you'd have to use one of the circular pointer solutions already posted elsewhere in boost.
In particular, Gre Colvin's cyclic_ptr located, at least last year, at libs/smart_ptr/smarttest.zip.
On the other hand, I'm working on another program which contains Colvin's and a variation of Colvin's
which avoids some problems. I'll upload the code today, but it won't have any documentation, except
in-source. I'll get that done later.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk