访问boost::graph中std::shared_ptr的成员函数
Accessing member function of std::shared_ptr in boost::graph?
我正在努力将boost::graph
算法的用法转换为一组新的实现类。我想知道:如果boost::graph
只存储std::shared_ptr
引用,是否可以访问对象的属性?类似于以下内容:
class Vert {
public:
Vert();
Vert(std::string n);
std::string getName() const;
void setName( std::string const& n );
private:
std::string name;
};
typedef std::shared_ptr<Vert> Vert_ptr;
using namespace boost;
typedef boost::adjacency_list<vecS, vecS, directedS, Vert_ptr> Graph;
Graph g;
Vert_ptr a( new Vert("a"));
add_vertex( a, g );
std::ofstream dot("test.dot");
write_graphviz( dot, g, make_label_writer(boost::get(&Vert::getName,g))); //ERROR!
是否可以访问std::shared_ptr
的成员以在图形标签编写器write_graphviz
或实现中的任何其他属性中使用?
谢谢!
是的,只需使用转换属性映射
在Coliru上直播
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <fstream>
#include <memory>
using namespace boost;
class Vert {
public:
Vert(std::string n="") : name(n) { }
std::string getName() const { return name; }
void setName( std::string const& n ) { name = n; }
private:
std::string name;
};
typedef std::shared_ptr<Vert> Vert_ptr;
struct Name { std::string operator()(Vert_ptr const& sp) const { return sp->getName(); } };
int main() {
typedef boost::adjacency_list<vecS, vecS, directedS, Vert_ptr> Graph;
Graph g;
Vert_ptr a( new Vert("a"));
add_vertex( a, g );
std::ofstream dot("test.dot");
auto name = boost::make_transform_value_property_map(Name{}, get(vertex_bundle,g));
write_graphviz( dot, g, make_label_writer(name));
}
结果:
digraph G {
0[label=a];
}
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 助记符和指向成员语法的指针
- 用于访问容器<T>数据成员的正确 API
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 嵌套在类中时无法设置成员数据
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将函数类成员映射到类本身内部
- 初始pointCloudt :: PTR类成员
- 通过成员函数将唯一 ptr 的向量附加到另一个向量
- LNK 2001 未解析的外部在 NPPluginFuncs 结构 ptr 作为静态类成员
- 我应该通过引用、值或ptr来存储一个完全封装的成员吗
- 从虚拟成员函数ptr中进行类型推导(bug?)
- 一个唯一的ptr类如何将它的实例指针传递给它的成员对象
- ptr到成员模板参数的友元语法