
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.

I believe you can do what you want like this: #include <boost\shared_ptr.hpp> #include <boost\weak_ptr.hpp> class A; class B; typedef boost::shared_ptr<A> spA; typedef boost::shared_ptr<B> spB; typedef boost::weak_ptr<A> wpB; class A { // ... spB m_b; }; class B { wsB m_wpParent; // ... std::vector<spA> m_a; }; I hope this helps, Manos From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Olivier Tournaire Sent: Thursday, May 10, 2007 9:08 PM To: boost-users@lists.boost.org Subject: [Boost-users] Smart pointers Hi all Suppose I have two classes A and B like those ones : Typedef 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.

________________________________________ 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: boost-users@lists.boost.org Subject: [Boost-users] Smart pointers 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 ? [Nat] Without thinking about it too hard... My first implementation would use std::vector< boost::shared_ptr<A> > for m_a (parent object responsible for lifespan of its children) and boost::weak_ptr<B> for m_b (child object not responsible for lifespan of its parent).

#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: boost-users@lists.boost.org 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.
participants (4)
-
Andrew Holden
-
Nat Goodspeed
-
Olivier Tournaire
-
TOL Tsagarakis Manos