数据模型、通用容器、C++

发布于 2024-11-05 12:11:47 字数 1055 浏览 5 评论 0原文

我正在编写一个新容器作为存储在 AContainer (抽象类)中的常见 STL 容器的扩展。对于这个容器,我想根据存储的对象(点、线等几何对象)添加新功能...

但问题有点复杂。例如...有多种类型的点(2D、3D...),大多数属性都是相同的... 哪种数据模型更合适:

1) 通用容器 (Universal Container)

从每种类型的对象通用的抽象类 AContainer 派生出的新类 Container。

AContainer -> Container

对其他几何对象执行部分专业化

Container <Point>
Container <Edge>
Container <Polygons>

,并在这些类中实现它们的行为。

但这种模型在描述不同类型点的相同行为时带来了麻烦。我必须专门化 Point2D、point3D 的容器

Container <Point2D>
Container <Point3D>

,并在这两个专门化中编写它们的行为,因此很多代码将被重复。

在我看来,在 C++ 中不可能对类和所有派生类进行部分特化。

2) 每种类型的对象的特定容器

每种类型的对象的新类的派生

AContainer ->ContainerPoints
           ->ContsinerEdges
           ->ContainerFaces

有几种可以专门化的不同容器。点行为描述的问题 不会发生,它们的共同属性将在内部定义,

ContainerPoints

而不需要任何专门化...

我担心代码太多碎片(太多相似的类)...例如 std::vector 对于每个都是通用的存储类型...

对于每个几何类型来说,创建一个新类或进行部分特化哪个更好?

感谢您的帮助...

I am writing a new container as an extension of common STL containers stored in AContainer (an abstract class). For this container I want to add new features depending on stored objects (geometric objects like points, lines, ...)...

But the problem is a little more complicated. For example... There are several types of points (2D, 3D...), most properties are the same ...
Which data model is more appropriate:

1) Universal Container

A derivation of the new class Container from abstract class AContainer common for each type of objects.

AContainer -> Container

Perform partial specialization for other geomeric objects

Container <Point>
Container <Edge>
Container <Polygons>

and implement their behavior inside those classes.

But this model brings troubles with the desciption of the same behavior of different types points. I have to specialize Container for Point2D, point3D

Container <Point2D>
Container <Point3D>

and in both specialization write their behavior, so a lot of code will be duplicated.

In C++ is, in my opinion not possible to do partial specialization for class and all derived classes.

2) Specific container for each type of object

A derivation of new classes for each type of object

AContainer ->ContainerPoints
           ->ContsinerEdges
           ->ContainerFaces

There are several different containers that could be specialized. The problem with the description of points behavior
will not occur, their common properties will be defined inside

ContainerPoints

without need for any specialization...

I'm afraid of too much fragmentation of the code (too much similar classes)... And for example std::vector is universal for each stored type...

Is it better for each geometric type to create a new class or to do a partial specialization?

Thanks for your help...

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

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

发布评论

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

评论(1

少跟Wǒ拽 2024-11-12 12:11:47

这个怎么样:

  template<class T>
  class Container { ... };
  class PointContainer : public Container<Point> { };

How about this:

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