M
md
Hi,
the following code is working for static objects.
ie the statement IntArray x(20);
my problem is i want to use this overloading operator [] for
dynamically created objects
for example the statement IntArray *x;
Please give me some help regarding this
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class IntArray
{
public:
IntArray(int size = 1); // default size: 1 int
IntArray(IntArray const &other);
~IntArray();
IntArray const &operator=(IntArray const &other);
// overloaded index operators:
int &operator[](int index); // first
int operator[](int index) const; // second
private:
void destroy(); // standard functions
// used to copy/destroy
void copy(IntArray const &other);
int
*data,
size;
};
IntArray::IntArray(int sz)
{
if (sz < 1)
{
cout << "IntArray: size of array must be >= 1, not " << sz
<< "!" << endl;
exit(1);
}
// remember size, create array
size = sz;
data = new int [sz];
}
// copy constructor
IntArray::IntArray(IntArray const &other)
{
copy(other);
}
// destructor
IntArray::~IntArray()
{
delete [] data;
}
// overloaded assignment
IntArray const &IntArray:perator=(IntArray const &other)
{
// take action only when no auto-assignment
if (this != &other)
{
delete [] data;
copy(other);
}
return (*this);
}
// copy() primitive
void IntArray::copy(IntArray const &other)
{
// set size
size = other.size;
// create array
data = new int [size];
// copy other's values
for (register int i = 0; i < size; i++)
data = other.data;
}
// here is the overloaded array operator
int &IntArray:perator[](int index)
{
// check for array boundary over/underflow
if (index < 0 || index >= size)
{
cout << "IntArray: boundary overflow or underflow, index =
"
<< index << ", should range from 0 to " << size - 1 <<
endl;
exit(1);
}
return (data[index]); // emit the reference
}
int main()
{
IntArray x(20); // 20 ints //statement 1
for (int i = 0; i < 20; i++)
x = i * 2; // assign the elements
// produces boundary
// overflow
for (int i = 0; i <= 20; i++)
cout << "At index " << i << ": value is " << x << endl;
return (0);
}
///////////////////////////////////////////////////////////////////////////////////////
With Warm Regards,
Murali.
the following code is working for static objects.
ie the statement IntArray x(20);
my problem is i want to use this overloading operator [] for
dynamically created objects
for example the statement IntArray *x;
Please give me some help regarding this
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class IntArray
{
public:
IntArray(int size = 1); // default size: 1 int
IntArray(IntArray const &other);
~IntArray();
IntArray const &operator=(IntArray const &other);
// overloaded index operators:
int &operator[](int index); // first
int operator[](int index) const; // second
private:
void destroy(); // standard functions
// used to copy/destroy
void copy(IntArray const &other);
int
*data,
size;
};
IntArray::IntArray(int sz)
{
if (sz < 1)
{
cout << "IntArray: size of array must be >= 1, not " << sz
<< "!" << endl;
exit(1);
}
// remember size, create array
size = sz;
data = new int [sz];
}
// copy constructor
IntArray::IntArray(IntArray const &other)
{
copy(other);
}
// destructor
IntArray::~IntArray()
{
delete [] data;
}
// overloaded assignment
IntArray const &IntArray:perator=(IntArray const &other)
{
// take action only when no auto-assignment
if (this != &other)
{
delete [] data;
copy(other);
}
return (*this);
}
// copy() primitive
void IntArray::copy(IntArray const &other)
{
// set size
size = other.size;
// create array
data = new int [size];
// copy other's values
for (register int i = 0; i < size; i++)
data = other.data;
}
// here is the overloaded array operator
int &IntArray:perator[](int index)
{
// check for array boundary over/underflow
if (index < 0 || index >= size)
{
cout << "IntArray: boundary overflow or underflow, index =
"
<< index << ", should range from 0 to " << size - 1 <<
endl;
exit(1);
}
return (data[index]); // emit the reference
}
int main()
{
IntArray x(20); // 20 ints //statement 1
for (int i = 0; i < 20; i++)
x = i * 2; // assign the elements
// produces boundary
// overflow
for (int i = 0; i <= 20; i++)
cout << "At index " << i << ": value is " << x << endl;
return (0);
}
///////////////////////////////////////////////////////////////////////////////////////
With Warm Regards,
Murali.