|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2005-06-06 11:08:15
Lorenzo Bettini <bettini_at_[hidden]> writes:
> Jonathan Wakely wrote:
>> On Mon, Jun 06, 2005 at 03:58:41PM +0200, Lorenzo Bettini wrote:
>>
>>>However, it is not clear to me how to use weak_ptr in this context...
>>>
>>>can some one provide some clue, please?
>> Well, for this context:
>> class C;
>> typedef boost::shared_ptr<C> CP;
>> typedef boost::weak_ptr<C> WP;
>> class C
>> {
>> private:
>> string s;
>> WP wp;
>> public:
>> C(const string &ss, CP c = CP()) : s(ss), wp(c) {}
>> void set(CP c) { wp = c; }
>> ~C() { cout << "destroying C" << endl; }
>> };
>> Then main() would be unchanged.
>> That might not work for anything less trivial than your example,
>> since
>> you'd need to ensure the shared_ptrs that own the objects are still
>> around when you use the weak_ptrs.
>
> indeed, it only works for that simple main: the main problem I had is
> just "ensure the shared_ptrs that own the objects are still
> around when you use the weak_ptrs.".
>
> Moreover, I had to deal with mutual dependences, and this makes the
> whole thing worse...
Can you deal with deallocation of the whole network at once? It often
works to maintain the nodes (C objects) in a container like a deque
and let them link to one another with raw pointers.
Otherwise you may be in need of a real GC.
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk