c++ - Overload + operator for integer array, What's difference between two function const and without const at right? -
this class of array access array of integerset. need access pointer ptr of integerset getptr();
you need access capacity of integerset getcapacity();
getptr() const const integerset class.
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(); (int i=0;i<capacity_;i++) { ptr[i]=rptr1[i]; // ptr pointer of class integerset } return *this; } //this function friend of class integerset extern .. integerset operator +(const integerset & rhs1,const int& rhs2) { int capacity_=rhs1.getcapacity(); integerset temp(capacity_); int *rptr1=rhs1.getptr(); int *tptr=temp.getptr(); (int i=0;i<capacity_;i++) { tptr[i]=rptr1[i]+rhs2; } return temp; }
what's difference between 2 function int * getptr() const , int * getptr(); ?
Comments
Post a Comment