#include<iostream>#include<string>using namespace std;class Coordinate{public: void printX(); void printY();int x,y; };void Coordinate:: printY(){cout<<y<<endl;}void Coordinate:: printX(){cout<<x<<endl;}int main(void){Coordinate *coor1=new Coordinate();coor1->x=0;coor1->y=20;coor1->printX();coor1->printY();delete coor1;coor1=NULL;Coordinate *coor2=new Coordinate[5];for(int i=0;i<5;i++){coor2[i].x=0;coor2[i].y=20;coor2[i].printX();coor2[i].printY();}delete []coor2;coor2=NULL;system("pause");}
1 回答
已采纳
望远
TA贡献1017条经验 获得超1032个赞
Coordinate *coor1=new Coordinate();
coor1是指针数据类型,访问成员自然用->
Coordinate *coor2=new Coordinate[5];
这里的coor2是指针数组类型,指向分配的数组空间的首地址,其中通过coor2[i](i=0,1,2,3,4)的方式来访问数组元素,单个元素都是Coordinate类型而不是Coordinate *类型,访问成员自然用.
- 1 回答
- 0 关注
- 1202 浏览
添加回答
举报
0/150
提交
取消