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)
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 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.
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: