【发布时间】:2016-08-21 22:51:11
【问题描述】:
class boundaryPt{
public:
friend class KCurvature;
int x;
int y;
boundaryPt(int x, int y){
this->x = x;
this->y = y;
}
boundaryPt(){}
};
class KCurvature{
public:
boundaryPt* boundaryPtAry;
int numPts;
ifstream input;
KCurvature(char* inFile){
input.open(inFile);
input >> numPts;
boundaryPtAry = new boundaryPt[numPts];
}
void loadData(char* inFile){
input.open(inFile);
int x;
int y;
while(!input.eof()){
input >> x;
input >> y;
boundaryPtAry[index++] = new boundaryPt(x,y);
}
};
我的问题是:
boundaryPtAry[index++] = new boundaryPt(x,y);
我正在尝试将我的boundaryPt 对象存储在我的boundaryPt 类型数组中,但由于我将该数组声明为boundaryPt*,它不会让我存储boundaryPt。
这是延迟指针的简单问题吗?我对 C++ 生疏了。
【问题讨论】:
-
boundaryPtAry[index].x = x; boundaryPtAry[index].y = y; index++;怎么样? -
那行得通。我现在意识到,当我创建一个对象数组时,它实际上创建了实际的对象。所以甚至不需要创建一个新的边界点对象。谢谢!
-
@user5904091 请将您的答案作为答案发布,而不是更新问题本身。
标签: c++ arrays pointers object