我应该在这里使用指针吗?
让我们创建三个类:
Vector3d 类:
class Vector3d{
int a,b,c;
}
类 Face,每个 Face 包含 3 个向量:
class Face{
Vector3d *a, *b, *c;
}
和类Mesh(3d对象):在类mesh中我只能获得面的向量,但是Vector3d类的大多数对象将在许多面之间共享,所以我想将Vector3d存储在Mesh类中并仅将指针存储在Face对象中;
class Mesh{
std::vector<Vector3d> points;
std::vector<Face> faces;
}
问题是,我不明白什么时候应该删除 Face 类中的指针,我应该使用其他东西而不是这个吗?
编辑:3 int 只是为了解释我的概念,在最终版本中,Vector3d 类中会有更多数据
回答
Face应该存储索引,而不是指针。
即Face应该有三个成员:
unsigned a,b,c;
如果添加/删除/调整Vector3d矢量大小,索引是安全的。
它还允许您以更方便的方式在面之间共享点。
这也意味着您可以将faces向量的内容直接复制到图形 API 缓冲区中(假设类型匹配)。
- I'd use `std::size_t` or `std::ptrdiff_t` instead of `unsigned`, but your point is good.
- Then, a,b,c will be index of Vector3d in std::vector or what? Google leads me to array's tutorial