For completeness:
- Because shared_ptr<> takes "ownership" of the passed pointer, it is incorrect to create two shared_ptr<>s from the same raw pointer
- It is incorrect to create cyclical loops via shared_ptr (at least if you actually want to prevent memory leaks). The relationship would need to be parent/child, with the child holding a non-shared pointer (e.g. a weak_ptr<> or possibly a raw pointer if you are confident) to the parent.
So you means two or more shared_ptr can not point to the same raw pointer?
But i found some description about shared_ptr like "
Object ownership shared among multiple pointers."
How to understand it?
And actually, I want do something like:
class Object
{
public:
void addComponents(boost::shared_ptr<Component> comp)
{
m_components.push_back(comp);
}
vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager : public boost::enable_shared_from_this<ObjectManager>
{
public:
void addComponent(boost::shared_ptr<Component> comp)
{
comp->m_Manager=shared_from_this();
m_allComponents.push_back(comp);
m_allObjects[some_id].addComponents(comp);
}
vector< boost::shared_ptr<Component> >m_allComponents;
vector< boost::shared_ptr<Object> > m_allObjects;
};
class Component
{
public
boost::shared_ptr<ObjectManager> m_Manager;
};
Some Components in Object::m_components are point the same Component in ObjectManager::m_allcomponents.
So it is incorrect?