采用堆分配空间时,结果一致,但为啥会报警告?
#include <iostream>
using namespace std;
class Coordinate
{
public:
Coordinate()
{
}
// 打印坐标的函数
void printInfo()
{
cout << "(" << m_iX <<","<< m_iY << ")" << endl;
}
public:
int m_iX;
int m_iY;
};
int main(void)
{
//定义对象数
Coordinate *p = new Coordinate[2];
p[0].m_iX=1;
p->m_iY=2;
p++;
p[0].m_iX=3;
p->m_iY=4;
p--;
//遍历数组,打印对象信息
for(int i = 0; i < 2; i++)
{
p[i].printInfo();
}
p--;
delete []p;
p=NULL;
return 0;
}
运行结果:
/bin/bash: line 1: 12644 Segmentation fault (core dumped) index (1,2) (3,4)