Chapter 2
if (ref($other) and $other >isa( Even )) {
# another Even object will always be even, so the addition
# can always be done
$result = $$self + $$other;
} else {
# make sure it s even
$other += 1 if $other % 2;
$result = $$self + $other;
}
# return a new object in the same class as $self
return ref($self) >new($result);
}
This method will work with other objects that are either of the Even class or inherit
from it. It also uses an inheritance safe method for creating new objects by calling the
new() method (implemented earlier in this section) on the result of calling ref() on the
object. This means that the new() method will be called on whatever class $self
belongs to, even if that's a child class of the class where add() is implemented.
Auto Generation
As you can see from the preceding example, it takes a lot of work to write a safe
overload method. Fortunately, it's usually not necessary to create methods for all
the possible overloadable operations. This is because the overload module can
auto generate many overload methods from existing overloaded methods. Table
2 4 contains the rules for method auto generation.
Table 2 4. Method Auto Generation
Method(s)
Auto Generation Description
Assignment forms of math Can be auto generated from the nonassignment forms
operators
(+= can be auto generated from +, for example)
Conversion operators
Any conversion operator can be auto generated from
any other
++,
Auto generated from += and =
abs()
Auto generated from < and binary subtraction
Unary
Auto generated from subtraction
Negation
Auto generated from Boolean conversion
Concatenation
Auto generated from string conversion
54
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved