我对曾经很好分开的 2 个班级有疑问,但现在他们想结合。
无需过多讨论问题的细节,这里是:
我曾经有一个包含 3 个空间位置顶点的三角形类。
class Triangle
{
Vertex a,b,c ; // vertices a, b and c
} ;
程序中有许多 Triangle 实例,因此每个实例都保留了自己的顶点副本。诸如 等的成员函数getArea()
写getCentroid()
在类中Triangle
,并且由于每个Triangle
实例都有顶点 a、b 和 c 的副本,因此查找区域或质心不依赖于其他类。应该是这样!
然后,出于其他原因,我想转向顶点数组/索引缓冲区样式表示。这意味着所有顶点都存储在位于Scene
对象中的单个数组中,并且每个Triangle
顶点仅保留对 in 顶点的引用Scene
,而不是顶点本身的副本。起初,我尝试切换出指针:
class Scene
{
std::vector<Vertex> masterVertexList ;
} ;
class Triangle
{
Vertex *a,*b,*c ; // vertices a, b and c are pointers
// into the Scene object's master vertex list
} ;
(如果您想知道好处,我这样做的原因主要是与共享顶点的三角形有关。如果 *a 移动,则使用该顶点的所有三角形都会自动更新)。
这将是一个非常好的解决方案!但它不能可靠地工作,因为 std::vector 使指针无效,并且我在 class 中使用 std::vector 作为主顶点列表Scene
。
所以我不得不使用整数:
class Triangle
{
int a,b,c ; // integer index values
// into the Scene object's master vertex list
} ;
但是现在我遇到了这个新的耦合问题:要找到自己的区域或质心,类Triangle
需要访问class Scene
之前没有的地方。好像我已经 fsck`d 了一些东西,但不是真的。
世界青年会?