为何coor1用->,coor2要用. ????????????

#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");

}


慕粉2347137632
浏览 1209回答 1
1回答

望远

Coordinate  *coor1=new Coordinate();coor1是指针数据类型,访问成员自然用->Coordinate *coor2=new Coordinate[5];这里的coor2是指针数组类型,指向分配的数组空间的首地址,其中通过coor2[i](i=0,1,2,3,4)的方式来访问数组元素,单个元素都是Coordinate类型而不是Coordinate *类型,访问成员自然用.
打开App,查看更多内容
随时随地看视频慕课网APP