升压图是否切割了我的shared_ptr

时间:2017-12-22 19:10:29

标签: c++ boost graph shared-ptr object-slicing

我使用boost::shared_ptr<Obj>作为边缘属性的增强图的实现。我有两个课程ObjObj2,以便:

class Obj{
    public:
    Obj(){};
    virtual func(){std::cout << "Obj func " << std::endl};
};

class Obj2: public Obj{
    public:
    Obj2(){};
    virtual func(){std::cout << "Obj2 func " << std::endl};
};

我使用(简化)函数在图表中添加边缘,例如:

void addEdge(Vertex index, Vertex index2, const boost::shared_ptr<Obj>& edgeAttribute){
    out = boost::add_edge(index, index2, edgeAttribute, graph).first;
}

我打电话使用:

Obj2* obj = new Obj2();
boost::shared_ptr<Obj2> obj_ptr(obj);
addEdge(index, index2, obj_ptr);

然而,稍后在我的代码中,通过执行edgeAttr = graph[edge]然后调用函数edgeAttr->func()来获取edge属性时,我会调用Obj的func而不是Obj2。

据我了解,这意味着我的对象被切片。这个简短的例子是我在使用boost:shared_ptr和BGL时应该切片我的对象,还是像初始化那样是我自己的其他一些实现问题?

1 个答案:

答案 0 :(得分:1)

图形模型没有切片任何属性(注意,“属性”实际上称为捆绑属性)。

事实上,它永远不会复制 ObjObj2,因为它只会复制导致共享所有权的shared_ptr<>,而不是复制属性

让我们展示它是如何工作的

<强> Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <iostream>

class Obj {
  public:
    Obj(){};
    virtual void func(std::ostream& os) { os << "Obj  func\n"; };
};

class Obj2 : public Obj {
  public:
    Obj2(){};
    virtual void func(std::ostream& os) { os << "Obj2 func\n"; };
};
// I'm adding edges in the graph using a (simplified) function such as:

using Graph = boost::adjacency_list<boost::vecS, boost::vecS,
      boost::directedS, boost::no_property, boost::shared_ptr<Obj> >;
using Vertex = Graph::vertex_descriptor;

struct Program {
    Program() : _graph(10) {}

    void addEdge(Vertex index, Vertex index2, const boost::shared_ptr<Obj> &edgeAttribute) {
        auto out = boost::add_edge(index, index2, edgeAttribute, _graph).first;
    }

    void sample_edges() {
        Obj2 *obj = new Obj2();
        boost::shared_ptr<Obj2> obj_ptr(obj);

        addEdge(1, 2, boost::make_shared<Obj2>());
        addEdge(2, 3, boost::make_shared<Obj >());
        addEdge(3, 4, boost::make_shared<Obj2>());
        addEdge(4, 5, boost::make_shared<Obj >());
        addEdge(5, 6, boost::make_shared<Obj2>());
        addEdge(6, 7, boost::make_shared<Obj >());
    }

    void debug_dump() const {
        for (auto ed : boost::make_iterator_range(boost::edges(_graph))) {
            _graph[ed]->func(std::cout << "Edge " << ed << ": ");
        }
    }

  private:
    Graph _graph;
};

int main() {
    std::cout << "Demo edges:\n";
    Program demo;
    demo.sample_edges();
    demo.debug_dump();

    std::cout << "Copied edges:\n";
    // copy the whole shebang
    Program clone = demo;
    clone.debug_dump();
}

打印:

Demo edges:
Edge (1,2): Obj2 func
Edge (2,3): Obj  func
Edge (3,4): Obj2 func
Edge (4,5): Obj  func
Edge (5,6): Obj2 func
Edge (6,7): Obj  func
Copied edges:
Edge (1,2): Obj2 func
Edge (2,3): Obj  func
Edge (3,4): Obj2 func
Edge (4,5): Obj  func
Edge (5,6): Obj2 func
Edge (6,7): Obj  func