这是访问IntegerSet数组的数组类。 您需要访问IntegerSet的指针 ptr GetPtr();
您需要访问IntegerSet的容量 GetCapacity();
GetPtr()const 到const IntegerSet类。
class IntegerSet
{
private:
int capacity;
int nelements;
int * ptr;
public :
IntegerSet(int size);
~IntegerSet();
int * GetPtr() const {return ptr;};
int Getcapacity() const {return capacity;};
IntegerSet & operator =(const IntegerSet & rhs1);
friend IntegerSet operator +(const IntegerSet & rhs1,const IntegerSet & rhs2);
};
IntegerSet::IntegerSet(int size)
{
ptr=null;
capacity = size;
ptr = new int [capacity];
if(ptr==null) cout << "error allocation"<< endl;
}
~IntegerSet()
{
if (ptr) delete ptr;
}
IntegerSet & IntegerSet::operator =(const IntegerSet & rhs1)
{
int capacity_=rhs1.Getcapacity();
int *rptr1=rhs1.GetPtr();
for (int i=0;i<capacity_;i++)
{
ptr[i]=rptr1[i]; // ptr is pointer of class IntegerSet
}
return *this;
}
//this function friend of class IntegerSet is extern ..
IntegerSet operator +(const IntegerSet & rhs1,const int& rhs2)
{
int capacity_=rhs1.Getcapacity();
IntegerSet temp(capacity_);
int *rptr1=rhs1.GetPtr();
int *tptr=temp.GetPtr();
for (int i=0;i<capacity_;i++)
{
tptr[i]=rptr1[i]+rhs2;
}
return temp;
}
两个函数 int * GetPtr()const 和 int * GetPtr(); 之间有什么区别?