Before looking at the implementation details, lets see what are the conventions that need to be followed if we want to
overload an operator.
- The operator function should be a member function of the containing type.
- The operator function must be
static
. - The operator function must have the keyword operator followed by the operator to be overridden.
- The arguments of the function are the operands.
- The return value of the function is the result of the operation.
Hide Copy Code
class A
{
public static A operator+(A a1, A a2)
{
A temp;
// perform actual addition
return temp;
}
}
// so the operations like
A a1 = new A();
A a2 = new A();
A result = a1 + a2;
// This can actually be visialized as:
A result = A.+(a1, a2);
// Note: Only for understnading, this wont even compile
No comments:
Post a Comment