是将我的shared_ptr切片的boost图
Is boost graph slicing my shared_ptr
我正在使用带有boost::shared_ptr<Obj>
的Boost图作为边缘属性的实现。我有两个Obj
和Obj2
类,因此:
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()
,我称obj的func而不是OBJ2。
这意味着我的物体被切成薄片。我给出的一个简短示例应该在使用Boost时切成我的对象:shared_ptr和bgl,还是我自己的其他实现问题(例如初始化)?
no图形模型不会切成任何属性(请注意,"属性"实际上称为捆绑属性)。
实际上,它永远不会复制 Obj
或 Obj2
,因为它只是复制了shared_ptr<>
,而不是复制共享所有权,而不是复制属性。
让我们演示它的工作原理
活在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 funcn"; };
};
class Obj2 : public Obj {
public:
Obj2(){};
virtual void func(std::ostream& os) { os << "Obj2 funcn"; };
};
// 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
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何在boost beast http请求中设置http头
- Boost Spirit,获取迭代器内部语义动作
- boost::asio::steady_timer()与sleep()我应该使用哪一个
- boost::asio如何生成多个协同程序,然后加入它们
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 使用Boost Interprocess创建托管共享内存需要很长时间
- 是将我的shared_ptr切片的boost图
- 如何在boost::Python中处理Python切片对象