【发布时间】:2009-12-01 21:39:47
【问题描述】:
我注意到这让一些人感到困惑,但在阅读了这里的几篇帖子和 cplusplus 教程后,我的大脑仍然混乱。
假设我在头文件中有以下变量-
int numberOfLinePoints;
D3DXVECTOR3* line; //confused as to what it is
然后在实现 C++ 文件中我将它们初始化如下 -
//both initialized in constructor
numberOfLinePoints = 25;
line = new D3DXVECTOR3[numPoints]; //array of pointers?
我的 line 变量现在代表什么?
据我从阅读 stackoverflow 上的链接可以看出,它应该代表一个指针数组。然后我阅读了以下内容......
...其中 (A) 指针数组和 (B) 指向数组的指针都进行了讨论。这让我再次感到困惑,因为它们的工作方式似乎相似。
我在一个单独的位置定义我的指针到我分配它们的位置(正确吗?)这一事实似乎是我困惑的根源。 我是否正确,这是指向 D3DXVECTOR3 对象的指针数组?
完成 - 如果变量 line 包含有关一条线段的信息,我将如何创建一个线段数组?我目前有以下 -
//HEADER FILE
int numberOfLineSegments;
D3DXVECTOR3** lineCollection; //array of pointers - each of which
//points to an array of pointers?
//CPP FILE
numberOfLineSegments = 8; //constructor
for(i = 0; i < numberOfLineSegments; i++) //initialization
{ //and allocation CORRECT?
lineCollection[i] = new D3DXVECTOR*[numPoints]; //of memory for Y/N
} //lineCollection
VOID createLineSegments(startPoint, endPoint) //could return array instead
{
//pseudo to generate one line segment
while != numberOfLinePoints
line[sentinel++] = interpolate(startPoint, endPoint, time_T)
//pseudo to generate array of line segments
while != numberOfLines
lineCollection[sentinel++] = line
}
非常感谢任何帮助。
【问题讨论】:
-
你永远不会在头文件中声明变量的存储。您的“头文件”行必须全部位于 CPP 文件的顶部。标头用于声明事物存在的抽象潜力,而不是它们的实际存储,或者它们在您的实际程序中使用或存在的现实。
-
我不知道。在我的课程中,我一直被教导对象的声明放在对象头中,并在 CPP 文件中初始化。我会和我的一位导师一起提出这个问题,谢谢。