标签:
#include <IOSTREAM>
using namespace std;
/*堆中对象数组、对象指针数组*/
class stu
{
public:
void set(int x){i = x + 6;}
int get(){return i*i;};
private:
int i;
};
void main()
{
const int n = 1000;
stu * p = new stu[n];//在堆中定义了一个对象数组,并将其首地址传送给对象指针p.
int i;
stu * p0 = p;
for (i=0; i<n; i++)
{
p->set(i);
cout<<"p["<<i<<"]:"<<p->get()<<endl;
p++;
}
delete [] p0;//删除堆中对象数组;(注:此处p0不可换为p,因为p的值在for循环中已被改变,不再是数组的首地址了。)
const int m = 1000;
stu * p1[m]; //p1成了指针数组的数组名,是个指针常量
for (i=0; i<m; i++)
{
p1[i] = new stu;//数组p1中,每个元素都是个对象指针,故可以直接被new stu赋值。
// p1 = p1+i;//错误,p1是个数组名,是个指针常量
p1[i]->set(i+1);
cout<<"p1["<<i<<"]:"<<p1[i]->get()<<endl;
delete p1[i];
}
}标签:
原文地址:http://blog.csdn.net/u010738823/article/details/51330068