#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@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On
Behalf Of Olivier Tournaire
Sent: Thursday, May 10, 2007 2:08
PM
To:
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.