Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference class and overload his = operator instead of doing so in the array class.
Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference class and overload his = operator instead of doing so in the array class.
Originally posted by FillYourBrain
the return value isn't even used in this example:
anArray[ index ] = value;
so I have difficulty following you. returning *this accomplished nothing here.
Not quite, maybe I could not make myself clear enough.
The object MUST be of MyArray type, as operator = is redefined in this class. If operator [] would return an int, then operator = defined for int type would be called, not the one I WANTED to.
Just try out both cases, you'll see what happens.
[R]evolution!
Programming related articles, downloads, demos
What about my suggestion, why not write your *own* int reference class then?
The example presents also an alternative way of handling some situations which can cause errors like over indexing.Originally posted by lyx
Maybe do you absolutely have to have an example of the equal operator overloading. If so, then create a reference class and overload his = operator instead of doing so in the array class.
[R]evolution!
Programming related articles, downloads, demos
Yes, but this is the task of your array class which returns a int reference class which task is to overload the = operator, and nothing more. Is that right with you?
I'm planning to extend MyArray class to template class.Originally posted by lyx
What about my suggestion, why not write your *own* int reference class then?
Or should I write another template class for operator = for all the primary types (char, int, etc.) ?
I rather live with this small inconvenience - that assignements like int x = anArray[ index ] won't be supported / compiled.
I might change my mind
Last edited by Carlos; 09-24-2003 at 08:53 AM.
[R]evolution!
Programming related articles, downloads, demos
Then write a template for the element class as well.
[EDIT]Don't answer to my older posts, just to the newest ones.
wait, wait, wait. I see where the confusion lies. First of all..
anArray[ index ] = value;
is to set an "int" inside the array. you are supposed to return a reference to an int in operator[]. This would set the thing with value.
I misunderstood but the example is still wrong. It makes no sense to return a *this because that's not what the array contains. You can simply return a reference to the corrected int instead. operator= on the array class doesn't make sense as a solution.
in addition, setting the last element as an alternative to the incorrect one they asked for is not safe as it would overwrite their valid data.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
But FYB (could I call you that? ^^), he *wants* to overload a = operator...
but...
myArray = 4;
what does that mean? When you set an array equal to 4. what is that? Overloading operators is there to make things more readable, not more difficult to understand. really, it'll work that way (starts to become a state engine for no reason though) but he's writing a chapter in a book on operator overloading. It's incorrect to make an equals operator that makes no logical sense like that. that's all I'm saying. Also, it's more correct to return a reference to the thing you're actually expecting.
what would this give you?
int i = myArray[3];
it would return a myArray reference? not terribly helpful if you ask me!
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
Read all what have been posted, I said that I agree with you, however, if he really want that much his overloaded = operator, he should make a reference class. And that if he wants a template class, he should make both templates, that's all.
Ok, FillYourBrain, I got it. There's no need to overload operator =...Originally posted by FillYourBrain
wait, wait, wait. I see where the confusion lies. First of all..
anArray[ index ] = value;
is to set an "int" inside the array. you are supposed to return a reference to an int in operator[]. This would set the thing with value.
I misunderstood but the example is still wrong. It makes no sense to return a *this because that's not what the array contains. You can simply return a reference to the corrected int instead. operator= on the array class doesn't make sense as a solution.
Code:int& MyArray::operator [] ( int index_in ) { // range check if ( index_in > myLastIndex ) { myIndex = myLastIndex; } else { myIndex = index_in; } return myArray[myIndex]; }Of course, it's just a temporary workaround, I planned to do some error handling and logging here.in addition, setting the last element as an alternative to the incorrect one they asked for is not safe as it would overwrite their valid data.
[R]evolution!
Programming related articles, downloads, demos
yeah, carlos. Sorry I misunderstood you for about a whole page. The proposal that you were making was strange and unexpected so I went off in another direction.
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
All those words for finally you accepting that the operator isn't needed...
I believed you WANTED to write your own operator, didn't you?The object MUST be of MyArray type, as operator = is redefined in this class. If operator [] would return an int, then operator = defined for int type would be called, not the one I WANTED to.
he could certainly justify an operator=(MyArray & arr). It makes sense to set an array equal to an array. That would be a good example. Not that Carlos is looking for examples from me
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter