从原始指针到 boost::shared_ptr:使用 share_from_this 实现树的转换



这很可能是一个初学者的问题。我实现了树的基本版本,节点存储在A类中,每个A都有一个父节点(根节点除外)和一个子向量。使用原始指针,一切正常:

#include <iostream>
#include <vector>
class A
{
private:
    class A* parent;
    std::vector<class  A*> children;
    std::vector<int> storage;
public:
    std::vector<int>& getStorage();
    void connect(A* child);
    std::vector<A*>& getChildren();
    void addStorage(int& i);
    A();
    A(A* par);
};

class B
{
private:
    A* root;
public:
    B();
    A*&  getRoot();
};
void A::addStorage(int& i)
{
    storage.push_back(i);
}
std::vector<int>&
A::getStorage()
{
    return storage;
}
B::B()
{
    A* a_1_2 = new A;
    root = a_1_2;
    //A* ptr1 = new A;
    //*ptr1 = root;
    A* a_2_3  = new A(a_1_2);
    //A* ptr2 = new A;
    //*ptr2 = a_2_3;
    A* a_2_13 = new A(a_1_2);
    //A* ptr3 = new A;
    //*ptr3 = a_2_13;
    A* a_13_14 = new A(a_2_13);
    //A* ptr4 = new A;
    //*ptr4 = a_13_14;
    A* a_13_24 = new A(a_2_13);
    int i =1;
    int j =2;
    int k =3;
    a_13_24->addStorage(i);
    a_13_24->addStorage(j);
    a_13_24->addStorage(k);
    //A* ptr5 = new A;
    //*ptr5 = a_13_24;
    A* a_2_13_b = root->getChildren().at(1);
    std::cout << "inside constructor root size"<< root->getChildren().size() << "n";
    std::cout << "inside constructor 2.child size"<< a_2_13_b->getChildren().size() << "n";
}
A*&
B::getRoot()
{
    return root;
}
std::vector<A*>&
A::getChildren()
{
    return children;
}
A::A(A* par)
{
    parent = par;
    par->connect(this);
}
A::A()
{
}
void
A::connect(A* child)
{
    children.push_back(child);
}
int main()
{
    B tree;
    std::cout << "inside main root size"<< tree.getRoot()->getChildren().size()<< "n";
    A* a_2_13_b = tree.getRoot()->getChildren().at(1);
    A* a_13_24_b = a_2_13_b->getChildren().at(1);
    std::cout << "inside main 2.child size"<< a_2_13_b->getChildren().size() << "n";
    std::cout << "inside main leave storage"<< a_13_24_b->getStorage().size() << "n";
    return 0;
}

这是我第一个程序的第一步。我想将原始指针转换为提升共享指针。

#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/enable_shared_from_this.hpp>
class A : public boost::enable_shared_from_this<A>
{
private:
     boost::shared_ptr<A> parent;
    std::vector<boost::shared_ptr<A> > children;
    std::vector<int> storage;
public:
    std::vector<int>& getStorage();
    void connect();
    std::vector<boost::shared_ptr<A> >& getChildren();
    void addStorage(int& i);
    A();
    A(boost::shared_ptr<A> par);
};

class B
{
private:
    boost::shared_ptr<A> root;
public:
    B();
    boost::shared_ptr<A>&  getRoot();
};
void A::addStorage(int& i)
{
    storage.push_back(i);
}
std::vector<int>&
A::getStorage()
{
    return storage;
}
B::B()
{
    A* a_1_2 = new A;
    boost::shared_ptr<A> ptr_1(a_1_2);
    root = ptr_1;
    //A* ptr1 = new A;
    //*ptr1 = root;
    A* a_2_3  = new A(ptr_1);
    boost::shared_ptr<A> ptr_2(a_2_3);
    ptr_2->connect();
    //A* ptr2 = new A;
    //*ptr2 = a_2_3;
    A* a_2_13 = new A(ptr_1);
    boost::shared_ptr<A> ptr_3(a_2_13);
    ptr_3->connect();
    //A* ptr3 = new A;
    //*ptr3 = a_2_13;
    A* a_13_14 = new A(ptr_3);
    boost::shared_ptr<A> ptr_4(a_13_14);
    ptr_4->connect();
    //A* ptr4 = new A;
    //*ptr4 = a_13_14;
    A* a_13_24 = new A(ptr_3);
    boost::shared_ptr<A> ptr_5(a_13_24);
    ptr_5->connect();
    int i =1;
    int j =2;
    int k =3;
    ptr_5->addStorage(i);
    ptr_5->addStorage(j);
    ptr_5->addStorage(k);
    //A* ptr5 = new A;
    //*ptr5 = a_13_24;
    boost::shared_ptr<A> a_2_13_b = root->getChildren().at(1);
    std::cout << "inside constructor root size"<< root->getChildren().size() << "n";
    std::cout << "inside constructor 2.child size"<< a_2_13_b->getChildren().size() << "n";
}
boost::shared_ptr<A>&
B::getRoot()
{
    return root;
}
std::vector<boost::shared_ptr<A> >&
A::getChildren()
{
    return children;
}
A::A(boost::shared_ptr<A> par)
{
    parent = par;
    //par->connect();
}
A::A()
{
}
void
A::connect()
{
    std::vector<boost::shared_ptr<A> > childs = parent->getChildren();
    childs.push_back(shared_from_this());
}
int main()
{
    B tree;
    std::cout << "inside main root size"<< tree.getRoot()->getChildren().size()<< "n";
    boost::shared_ptr<A> a_2_13_b  = tree.getRoot()->getChildren().at(1);
    boost::shared_ptr<A> a_13_24_b = a_2_13_b->getChildren().at(1);
    std::cout << "inside main 2.child size"<< a_2_13_b->getChildren().size() << "n";
    std::cout << "inside main leave storage"<< a_13_24_b->getStorage().size() << "n";
    return 0;
}

根节点已经没有子节点,因此以下行

boost::shared_ptr<A> a_2_13_b = root->getChildren().at(1);

给:

terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check
Aborted (core dumped)

我不明白这一点,因为当我创建根子节点时,我调用了connect(),它应该将子节点存储在其父节点(这里是根)子节点中。

编辑:得益于帮助的工作解决方案:

#include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/enable_shared_from_this.hpp>
class A : public boost::enable_shared_from_this<A>
{
private:
     boost::weak_ptr<A> parent;
    std::vector<boost::shared_ptr<A> > children;
    std::vector<int> storage;
public:
    std::vector<int>& getStorage();
    void connect();
    std::vector<boost::shared_ptr<A> >& getChildren();
    void addStorage(const int& i);
    A();
    A(boost::weak_ptr<A> par);
};

class B
{
private:
    boost::shared_ptr<A> root;
public:
    B();
    boost::shared_ptr<A>&  getRoot();
};
void A::addStorage(const int& i)
{
    storage.push_back(i);
}
std::vector<int>&
A::getStorage()
{
    return storage;
}
B::B()
{
    //A a_1_2;
    boost::shared_ptr<A> ptr_1= boost::make_shared<A>();
    root = ptr_1;
    //A a_2_3 (ptr_1);
    boost::shared_ptr<A> ptr_2= boost::make_shared<A>(ptr_1);
    ptr_2->connect();
    //A a_2_13 (ptr_1);
    boost::shared_ptr<A>ptr_3 = boost::make_shared<A>(ptr_1);
    ptr_3->connect();
    //A a_13_14 (ptr_3);
    boost::shared_ptr<A> ptr_4= boost::make_shared<A>(ptr_3);
    ptr_4->connect();
    //A a_13_24 (ptr_3);
    boost::shared_ptr<A> ptr_5= boost::make_shared<A>(ptr_3);
    ptr_5->connect();
    ptr_5->addStorage(1);
    ptr_5->addStorage(2);
    ptr_5->addStorage(3);

    boost::shared_ptr<A> a_2_13_b = root->getChildren().at(1);
    std::cout << "inside constructor root size"<< root->getChildren().size() << "n";
    std::cout << "inside constructor 2.child size"<< a_2_13_b->getChildren().size() << "n";
}
boost::shared_ptr<A>&
B::getRoot()
{
    return root;
}
std::vector<boost::shared_ptr<A> >&
A::getChildren()
{
    return children;
}
A::A(boost::weak_ptr<A> par)
{
    parent = par;
    //par->connect();
}
A::A()
{
}
void
A::connect()
{
    auto par = parent.lock();
    //std::vector<boost::shared_ptr<A> > childs = par->getChildren();
    par->getChildren().push_back(shared_from_this());
}
int main()
{
    B tree;
    std::cout << "inside main root size"<< tree.getRoot()->getChildren().size()<< "n";
    boost::shared_ptr<A> a_2_13_b  = tree.getRoot()->getChildren().at(1);
    boost::shared_ptr<A> a_13_24_b = a_2_13_b->getChildren().at(1);
    std::cout << "inside main 2.child size"<< a_2_13_b->getChildren().size() << "n";
    std::cout << "inside main leave storage"<< a_13_24_b->getStorage().size() << "n";
    return 0;
}

这应该是一个参考:

void
A::connect()
{
    std::vector<boost::shared_ptr<A> > childs = parent->getChildren();
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

否则,您只需将子级连接到矢量的本地副本,然后将其丢弃即可。您永远不会将其添加到父级自己的向量中。

您可以直接添加它,而不是使用引用:

  parent->getChildren().push_back(shared_from_this());

另外,为什么A::addStorage(int&)只需要一个非常量引用,而它所做的只是复制它? 让它A::addStorage(int)B::B()其余部分也变得更简单:

    ptr_5->addStorage(1);
    ptr_5->addStorage(2);
    ptr_5->addStorage(3);

相关内容

  • 没有找到相关文章

最新更新