Boost Graph Library 多态捆绑属性

发布于 2024-12-27 14:42:48 字数 449 浏览 7 评论 0原文

因此,我使用以下类型的增强图:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost:directedS, VertexT, EdgeT> GraphT

VertexT 和 EdgeT 都是保留我需要的许多属性的类。这些是捆绑属性。我不确定我想使用 bgl 的某些方法是否可行,因此如果您熟悉它们,我们将不胜感激。

VertexT 和 EdgeT 被认为是多态基类。我的理解是 bgl 并不意味着用于指向这些属性的指针。如何使用 BGL 处理多态顶点和边属性?我想过使用共享指针,但我更喜欢自己管理内存。此外,这似乎可以防止使用 boost::get 生成 boost 布局的位置图时出现问题。

现在,我已经通过让顶点包含另一个指向真正多态类的指针来解决这个问题。但这似乎太复杂了。有什么建议吗?

So I'm using a boost graph of the following type:

typedef boost::adjacency_list<boost::listS, boost::vecS, boost:directedS, VertexT, EdgeT> GraphT

VertexT and EdgeT are both classes to keep many of the properties I need. These are bundled properties. I'm not sure if some of the ways I want to use bgl are possible so if you are familiar with them help would be much appreciated.

VertexT and EdgeT are suppose to be polymorphic base classes. My understanding is bgl is not meant to use for pointers to these properties. How does one work with polymorphic vertex and edge properties with BGL? I thought of using shared pointers but I'd prefer to manage the memory myself. Additionally this seems to prevent a problem when using boost::get to generate the position map for boost layouts.

Right now I've hacked my way around this by just having vertex contain another pointer to the true polymorphic class. But that seems too complex. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山人契 2025-01-03 14:42:48

在算法的通用实现中,最好使用值语义:复制对象会导致存在两个独立的相同对象。当需要复制对象时,这是至关重要的属性。动态多态性不能立即与值语义一起使用,因为要使用动态多态性,您需要处理指针或引用:使用值时,对象的静态类型和动态类型一致,这不允许直接使用动态多态性。

在这种情况下,处理动态多态对象的唯一方法是赋予它们有价值的外观和感觉。实际上,这意味着您需要将指向对象的指针封装到公开所需值接口的对象中(如果您坚持,您也可以封装引用,但我从未发现这效果很好)。 Boost Graph 库并不真正关心各种结构在内部如何表示,只要它们具有所需的接口并实现所需的语义即可。根据您的描述,使用指向多态对象的指针的包装器是正确的方法。无论您是通过标准智能指针之一还是以其他方式维护对象,都并不重要,尽管我猜想使用诸如 boost::shared_ptrstd::shared_ptr< 之类的东西;T> 消除了许多不必要的复杂性。

综上所述,我想指出的是,我很少找到动态多态对象与算法相结合的有用示例!是的,有一些,但大多数情况下,动态多态性的使用会导致问题,而不是解决方案(尽管许多只接触过面向对象技术的人会说;但是,如果您知道的唯一工具是锤子,每个问题看起来都像钉子)。

In generic implementations of algorithms it is preferred to use value semantics: copying an object causes two identical objects to exist which are independent. This is crucial property when it is necessary to duplicate objects. Dynamic polymorphism doesn't immediately work with value semantics because for the use of dynamic polymorphism you need to deal with pointers or reference: when using values, the static type and the dynamic type of object coincide which doesn't allow dynamic polymorphism directly.

The only way to deal with dynamically polymorph objects in this case is to give them a value look and feel. Effectively, this means that you need to encapsulate the pointers to your objects into object which expose the required value interface (you can also encapsulate references if you insist but I never found that this works well). The Boost Graph library doesn't really care about how the various structures are internally represented as long as they have the required interface and implement the required semantics. From what you describe using a wrapper for your pointers to the polymorphic objects is the right way to go. Whether you maintain the object via one of the standard smart pointers or differently doesn't really matter although I'd guess that using something like boost::shared_ptr<T> or std::shared_ptr<T> removes a number of unnecessary complications.

All this said, I'd like to point out that I rarely find useful examples of dynamically polymorph objects combined with algorithms! Yes, there are some but most of the time use of dynamic polymorphism is contributing to the problem, not to the solution (despite what many people having only exposure to object-oriented technique say; however, if the only tool you know is a hammer, every problem looks like a nail).

∞琼窗梦回ˉ 2025-01-03 14:42:48

我认为您基本上正在解决与此问题相同的问题:

仅适用于(捆绑)属性。我建议您可以通过调用独立的函数模板来实现多态性。


对于真正的高功率机械:

另请参阅本文:论面向对象编程和通用编程之间的紧张关系在 C++ 中;该论文描述了类型擦除,这是解耦/桥接运行时/静态多态性需求的最终“解决方案”。 (_请注意,如果您需要实现类型擦除,Boost Variant、Boost Any 等库会更方便)。

I think you are basically solving the same problem as this question:

only for (bundled) properties. I'd suggest you can do the polymorphism by invoking free-standing function templates.


For real high-power machinery:

See also this paper: On the Tension Between Object-Oriented and Generic Programming in C++; that paper describes type erasure which is kind of the end-all 'solution' to decouple/bridge your runtime/static polymorphism needs. (_Note that libraries such as Boost Variant, Boost Any are much more convenient if you need to implement type erasure).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文