내 프로그램에서 디자인 문제가 있습니다. 루트 ChainDescriptor의 일부인 Nodes 객체를 관리해야합니다.C++ : 원시 포인터를 공유 및 약한 ptr로 대체하십시오.
은 기본적으로는 다음과 같습니다
class ChainDescriptor
{
public:
~ChainDescriptor()
{
//delete the nodes in nodes...
}
void addNode(Node *);
Node * getNode();
const std::list<Node *>& getNodes() const;
std::list<Node *> m_nodes;
};
class Node
{
public:
Node(Node *parent);
void addChild(Node *node);
Node * getChild(const std::string& nodeName);
private:
Node * m_parent;
std::list<Node*> m_childs;
};
ChainDescriptor 클래스는 모든 노드를 소유하고 삭제의 책임이있다. 그러나 이러한 클래스는 이제 다른 프로그램, 즉 "소유권"문제가있는 실행 취소/다시 실행 기능이있는 GUI에서 사용해야합니다. 깊이 기존 코드를 수정하기 전에, 내가 고려하고 다른 솔루션 :
weak_ptr
위의 예에서
각각 list<weak_ptr<...> >
를 사용 shared_ptr
및 각각의 list<shared_ptr<...> >
shared_ptr
및 weak_ptr
을 올바르게 사용할 위치를 실제로 알지 못합니다. 의견이 있으십니까?
['boost :: ptr_list'] (http://www.boost.org/doc/libs)는 어떨까요? /1_55_0/libs/ptr_container/doc/ptr_list.html)? –