Overloading Operators

Hi friends,

I want to overload operators as part of an Array class in order to (1) Get a value and (2) Set a value, but checking the boundaries of this Array.

(1) I didn't have problems overloading the operator "[]" to Get values from the array:

double x = Array[pos]; // we need the position: pos

(2) I can't do the same for a complex operator "[]=" to Set values into the array:

Array [pos]= y; // we need the position and the new value: pos, y

Is there any solution to implement this complex operator?

Thanks for your help.

/ *****************************************************/

#include <iostream>
#include <vector>


using namespace std;

typedef double vtype;

class DataArray
{
public:
ArrayYes (int);
~ArrayYes () {delete [] pData;}

int operator[] (int aPos);
DataArray& operator [int aPos] = (vtype Val)
int GetaSize() const { return aSize; }
class xBoundary {};
private:
int aSize;
vtype *pData;
};

int main()
{
DataArray MyArray (100);
try
{
vtype x;
x = MyArray [80]; // Getting a value stored at the array

MyArray [20]= 220.5; // Setting a new value
}
catch (DataArray::xBoundary)
{
cout << "Error: position out of range." << endl;
cout << "Access available Array [" << "0," << (DataArray.GetaSize() - 1) << "]" << endl;
}
return 0;
}


DataArray :: DataArray(int defasize):aSize(defasize)
{
pData = new vtype [defasize];
for (int i=0; i<defasize; i++)
{
pData[i] = 10+i;
cout << "Value [" << i << "] = " << pData[i] << " ...loaded." << endl;
}
}

int DataArray :: operator[] (int aPos)
{
if (aPos>=0 && aPos<GetaSize())
{
cout << "\n\n" << "DataArray[" << aPos << "] = " << pData[aPos] << "\n\n" << "Done." << endl;
return aPos;
}
cout << "\n\n" << "Access denied to Array [" << aPos << "]" << endl;
throw xBoundary();
return 0;
}

//DataArray& DataArray :: operator [int aPos] = (vtype Val)
//{
// aPos = 8;
// if (aPos>=0 && aPos<GetaSize())
// {
// cout << "Old value: " << pData[aPos] << "\n";
// pData[aPos] = Val;
// cout << "New value: " << pData [aPos] << "\n" << "Done." << endl;
// return *this;
// }
// throw xBoundary();
// return *this;
//}
It would almost same as your get function but the difference is the latter is with a reference operator.

It would look like:

int & DataArray::operator [](int aPos)
{
return this->vType[aPos]; // or something you want to return the value of

}



Check it out. Good luck ;)
Or simpler more, you can make your getter to be combined a setter and getter. Meaning, you just change your current getter to return a reference, adding an ampersant to the return type.

Change the current
int DataArray :: operator[] (int aPos)

to
int & DataArray :: operator[] (int aPos)

That is it. You are good to go :)

I tried it, however it doesn't work. I hope you'll be luckier.
It should, otherwise you may be missing something.

Just add a reference to the return type and it makes it setter-cum-getter.

Good luck :)
operator[] should always return a reference, however, one should be const:

T& operator[](size_t pos);
const T& operator[](size_t pos) const;
Hello friends,

I thank you a lot for the answers posted. I apologize if my program quoted didn“t work. Maybe I introduced unexpected mistakes in class and data types when I simplified my original code to quote it here. It was not my intention.

Now, I am strong interested in implement this particular syntax: "[]=" to assign new values to my existing arrays, but extending additional capabilites at the same time, like to check the boundaries and memory addresses.

There is a huge set of data arays where I work and to use a function call is a little bit problematic. The best way it could be use the "complex operator" mentioned.

myArray [pos]= newValue; // checking the assigning process at the same time

Is there any way to implement this? What should be the best option?

Thank you.
How about reading the answers to your post?!

(although you should change the int to at least an unsigned type)
Last edited on
Thank you exception, I have read them, but it was not clear to me the specific issue how to make two different operators "[]" and "=" to work together as one.

I appreciate your answers and I read them carefully.

Thank you again.
There is no such thing as the []= operator.

The solution to your problem is in exceptions penultimate post: have [] return a reference (which it should do anyway).

If the index is invalid, throw an exception.

Then, if the referenced object has a properly overloaded assignment operator, you can validate the value being assigned.

You cannot overload int::operator=() so you would have to supply a proxy object to do validations. It might be simpler (and more efficient) to just make an assignment method, like:

DataArray& assign( unsigned index, int value );

Good luck!
Topic archived. No new replies allowed.