|
Boost Users : |
From: Andrew Holden (aholden_at_[hidden])
Date: 2007-05-10 14:25:07
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
class A
{
// ...
boost::weak_ptr <B> m_b; //Use a weak pointer to break cycles
};
class B
{
// ...
std::vector<boost::shared_ptr <A> > m_a;
};
If B uses a member function to allocate A and/or add A to the vector, then B would become something like this:
#include <boost/enable_shared_from_this.hpp>
class B: public boost::enable_shared_from_this <B>
{
void add_an_A (boost::shared_ptr <A> new_a)
{
new_a->m_b = shared_from_this();
m_a.push_back (new_a);
}
// ...
std::vector<boost::shared_ptr <A> > m_a;
};
I would recommend reading the documentation at
http://www.boost.org/libs/smart_ptr/smart_ptr.htm
for more information.
________________________________
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Olivier Tournaire
Sent: Thursday, May 10, 2007 2:08 PM
To: boost-users_at_[hidden]
Subject: [Boost-users] Smart pointers
Hi all
Suppose I have two classes A and B like those ones :
class A
{
// ...
B *m_b;
};
class B
{
// ...
std::vector<A *> m_a;
};
m_b is used to know who is the parent of the object.
I would like to use smart pointers. How can I do this ?
Thanks in advance
-- Le temps des cerises reviendra. Dans l'immédiat, c'est le temps des noyaux. Courage.
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