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

时间:2014-11-20 15:14:01

标签: c++ pointers boost smart-pointers

这很可能是初学者的问题。 我实现了树的基本版本,节点存储在类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;
}

这是我第一个项目的第一步。我想将原始指针转换为boost共享指针。

#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;
}

1 个答案:

答案 0 :(得分:1)

这应该是一个参考:

void
A::connect()
{

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

否则,您只需将子项连接到向量的本地副本,然后将其丢弃即可。你永远不会把它添加到父亲自己的载体中。

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

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

另外,为什么A::addStorage(int&)需要非const引用才能复制它?设为A::addStorage(int)B::B()的其余部分也变得更简单了:

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