“实施的多样性” - 如何处理?还有“名字”是什么?这样的事?

发布于 2024-11-30 20:22:19 字数 595 浏览 0 评论 0原文

我的问题是关于编程技术或者设计模式。假设从基类派生的几个类包含一个纯虚拟方法,该方法应该在它们之间进行一些交互。

例如,矩形椭圆三角形直线 - 全部源自形状 >。这个抽象类 Shape 包含 virtual bool Intersects(Shape* another) = 0

看来,我需要进行六个实现,对吧(顺便说一句,是任何在这个特定的例子中更好的解决方案?)。

目前我不知道还有其他例子。

也许我正在谈论一件众所周知的事情,并且我几乎可以肯定有一些名称描述了该技术。但是,我什至不知道要输入什么才能在互联网上找到它。

好吧,你能告诉我如何实现这样的事情吗(我仍然想知道是否需要任何辅助方法,或者也许需要RTIIdynamic_cast?)或者点关于它的一些来源(艺术、教程或其他)?

My question is a bit about programming techniques or, maybe, design patterns. Suppose several classes derived from base which contains a pure virtual method that should do some interactions beetwen them.

For instance, Rectangle, Ellipse, Triangle and Line - all derived from a Shape. And this abstract class, Shape, contains virtual bool Intersects(Shape* another) = 0.

It seems, that I need to make six implementations, right (btw, is any better solution in this particular example?).

I have no idea of any other examples at the moment.

Maybe I'm talking about a thing that is well-known and I'm nearly sure there are some names describing the technique. However, I don't even know what to input to find it in the Internet.

Well, can you tell me how to implement such thing (I'm still wondering if any helper method is needed, or, maybe, RTII's dynamic_cast?) or point some sources (arts, tutorials or whatever) about it?

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

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

发布评论

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

评论(2

吹梦到西洲 2024-12-07 20:22:19

这是双重调度的教科书示例,维基百科文章对问题和解决方案给出了很好的描述:

http://en.wikipedia.org/wiki/Double_dispatch

如果我没记错的话,Andrei Alexandrescu 的《Modern C++ Design》一书中有一个非常优雅的解决方案。

http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/ dp/0201704315

This is the school book example of double dispatch, the wikipedia article gives a good description of the problem and the solution:

http://en.wikipedia.org/wiki/Double_dispatch

If I remember correctly there's a very elegant solution to the problem in the book "Modern C++ Design" by Andrei Alexandescu

http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315

孤独难免 2024-12-07 20:22:19

一个简单的事实是您错误地使用了继承。您不应该拥有 Line、Box、Triangle 类型的继承,而应该使用 VertexShape 来表示由一系列顶点组成的所有形状,以及 FormulaShape 来表示由数学公式描述的所有形状。

继承不应该用于对可以通过简单地改变类中必须存在的数据来建模的关系进行建模,而不管如何,例如用于构建多边形的顶点。

编辑:不,不,不。使用模板。如果你绝望的话就专攻它。这就是它的用途。提供零开销通用算法。伙计们,我们在 90 年代就发现了这个东西。

The simple fact is that you are using inheritance incorrectly. Instead of having a Line, Box, Triangle kind of inheritance, you should instead have a VertexShape which represents all shapes that are composed of a series of vertices, and a FormulaShape which represents all shapes which are described by mathematical formulae.

Inheritance should not be used to model relationships that can be modelled by simply varying data which must be present in the class regardless- for example, the vertices used to build a polygon.

Edit: No, no, no. Use a template. Specialize it if you're desperate. That's what it's for. To provide zero-overhead generic algorithms. We discovered this stuff in the 90s, guys.

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