I am just making sure that I get this logic correctly.
Please give me some input if I make mistakes.
This is in the Fraction.m file
-(Fraction *) add: (Fraction *) f
{
Fraction *result = [[Fraction alloc] init];
result.numerator = (numerator * f.denominator) + (denominator * f.numerator);
result.denominator = denominator * f.denominator;
[result reduce];
return result;
}
and the other part is in the main.m
for (i = 1; i <= n; ++i) {
[aFraction setTo: 1 over: pow2];
sum2 = [sum add: aFraction];
[sum release];
sum = sum2;
pow2 *= 2;
}
let's make example that 'i' in the main.m is 5, so the loop will be 5 times.
The 1st time, method '
add' will create object and put the reference to
result and return the reference to
sum2.
And then the original
sum is destroyed, after that the reference copy from
sum2 into
sum.
The 2nd time, the method '
add' will create NEW OBJECT at NEW MEMORY LOCATION and the reference store in the
result and return the reference into
sum2 which replace the previous reference
sum2 had.
The [sum release] will destroy the object that created by the method '
add' during the first loop (when i=1), after that
sum will get the reference from
sum2 (
result also pointing at the same object here too).
The point here is the [sum release] from the i=1 and i=2 destroying different thing. One is destroying the object created by the
setTo: over: method (the original
sum) and the 2nd one is the object created by
add method. Am I right here?
This process repeat until it fulfill the 'for' condition and then at the end of main.m we see another [sum release] which will destroy the object which
sum,
sum2, and
result all pointing at the same thing.
Forgive my English, if my post is not clear please let me know, I will try to rephrase the wording.
Thank you and please let me know all your comments.