Kornalius,
I'd say don't implement operator overloading at all. I'll give a short explanation.
Consider for a moment 2 objects A and B. To copy the value of B to A we would write A = B. (Stay with me, it gets better.) What does this statement do for the object? No one knows. The creator of the object would presumably overload the = operator and create a method that would do the copy. (In C++ the compiler will create a default action for the operator, but I will almost guarantee you that is will be wrong.)
Now imagine that this object contained an integer and a string. The overloaded = operator code might look like this:
- Code:
#class myObj$
public tmpStr$
public tmpInt$
// overloaded =
proc = (srcObj$, destObj$)
destObj.tmpInt$ = srcObj.tmpInt$;
destObj.tmpStr$ = srcObj.tmpStr$;
end;
#endclass
If this were C++, there would be a problem. In the string assignment, the address of the source string would be copied to the address for the destination string. Following the assignment, if the source string were deleted, then the destination string would have a reference to freed memory. So to make it work, you must allocate a new string object and set it to the chars from the source string and the destination object would then point to the new memory. So for a complex class the = operator would need to have a method with many steps.
Now extrapolate for the > operator. How to compare A with B? Compare lexically on the string and if equal look at the int? The other way around? The designer of the class decides because for a complex class, there may be many steps in determining which is greater.
So, with the background out of the way, why "no" to operator overloading? First, I think it would get
very complex to do it "right". Second, any lesser solution with a single getter and setter would just get in the way. Third, there is no implementation of private class variables.
Private vars (in my opinion) make operator overloading necessary to a certain extent. It would be impossible for some procedure or method outside the class to copy the exact state of the object if there was a private var with no getter and setter.
OK, maybe that wasn't so brief, but it sounded like you were a little fuzzy on how they actually worked in C++.
Come on gang. Speak up. Lets here some other opinions.
Brad