I just tried this out with this sample source:
Code:
#include <boost/shared_ptr.hpp>
#include <iostream>
#include <vector>
template<typename T> class Node
{
public:
Node(int Id, T* Obj): pObj(Obj) { m_Id = Id; std::cout << "Node constructing (#" << m_Id << ")...\n"; }
~Node() { std::cout << "Node destructing (#" << m_Id << ")...\n"; }
boost::shared_ptr< Node<T> > pNext;
boost::shared_ptr< Node<T> > pPrevious;
boost::shared_ptr< boost::shared_ptr< Node<T> > > pFirst;
boost::shared_ptr< boost::shared_ptr< Node<T> > > pEnd;
boost::shared_ptr<T> pObj;
private:
int m_Id;
};
class Object
{
public:
Object(int Id) { m_Id = Id; std::cout << "Object constructing (#" << m_Id << ")...\n"; }
~Object() { std::cout << "Object destructing (#" << m_Id << ")...\n"; }
private:
int m_Id;
};
int main()
{
std::vector< boost::shared_ptr< Node<Object> > > v;
for (int i = 0; i < 10; i++)
v.push_back
(
boost::shared_ptr< Node<Object> >
( new Node<Object>(i, new Object(i)) )
);
v[0]->pFirst = boost::shared_ptr< boost::shared_ptr< Node<Object> > >
( new boost::shared_ptr< Node<Object> >(v[0]) );
v[0]->pEnd = boost::shared_ptr< boost::shared_ptr< Node<Object> > >
( new boost::shared_ptr< Node<Object> >(v[9]) );
v[0]->pNext = v[1];
for (int i = 1; i <= 8; i++)
{
v[i]->pPrevious = v[i - 1];
v[i]->pNext = v[i + 1];
v[i]->pFirst = v[0]->pFirst;
v[i]->pEnd = v[0]->pEnd;
}
v[9]->pPrevious = v[8];
v[9]->pFirst = v[0]->pFirst;
v[9]->pEnd = v[0]->pEnd;
}
Essentially, a double-linked list with front and end markers with shared_ptrs.
And when I run it, I get this output:
Code:
Object constructing (#0)...
Node constructing (#0)...
Object constructing (#1)...
Node constructing (#1)...
Object constructing (#2)...
Node constructing (#2)...
Object constructing (#3)...
Node constructing (#3)...
Object constructing (#4)...
Node constructing (#4)...
Object constructing (#5)...
Node constructing (#5)...
Object constructing (#6)...
Node constructing (#6)...
Object constructing (#7)...
Node constructing (#7)...
Object constructing (#8)...
Node constructing (#8)...
Object constructing (#9)...
Node constructing (#9)...
Press any key to continue . . .
Oh, the horrors! Memory leaks all over the place!