Amazon.com Widgets Allocating and Returning Objects from Methods
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 23, 2013, 11:58:53 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 7
| | |-+  Allocating and Returning Objects from Methods
Pages: [1]   Go Down
Print
Author Topic: Allocating and Returning Objects from Methods  (Read 485 times)
Ahmed Al H
Newbie
*
Posts: 11







« on: July 17, 2012, 04:30:51 AM »

i didn't get why you didn't need to allocate and initialize resultFraction is it a different style or is it a must if the return type is an object and if you can give me a simple example because i am stuck in chapter 7 since december. another question why was 3/4 printed twice i can see only one print method being invoked.

Thank you
« Last Edit: July 17, 2012, 04:38:45 AM by Ahmed Al H » Logged
kefka
Newbie
*
Posts: 14


Email




« Reply #1 on: July 24, 2012, 02:42:25 PM »

Hi Ahmed,

I don't have an answer to your question regarding the non allocation and initialization of resultFraction. I just wanted to ask you if your code is also printing the 3/4 twice? In my code its only once but in the book its twice.  Maybe someone else could explain this?

Daniel
Logged
kefka
Newbie
*
Posts: 14


Email




« Reply #2 on: July 24, 2012, 02:51:59 PM »

Also regarding your first question I was thinking and researching maybe the explanation is that aFraction and bFraction both get their instance variables changed here:
Code: (Objective-C)
[aFraction setTo:1 over:4];//Set first fraction to 1/4
[bFraction setTo:1 over:2];//Set the second one to 1/2

However, resultFraction just gets completely replaced with another one(as below), so I think it is not necessary to allocate memory or initialize it.

Code: (Objective-C)
resultFraction = [aFraction add:bFraction];


You might find the below link to stack overflow.com useful!

Regards

Daniel

http://stackoverflow.com/questions/3801156/when-not-to-alloc-and-init-an-nsstring
Logged
kefka
Newbie
*
Posts: 14


Email




« Reply #3 on: July 24, 2012, 03:03:11 PM »

Of course it could also be the fact that......

Code: (Objective-C)
Fraction *result = [[Fraction alloc]init];

.......is included in the method implementation?...seems more likely.

Daniel
Logged
Ahmed Al H
Newbie
*
Posts: 11







« Reply #4 on: July 25, 2012, 08:00:57 AM »

Hi Ahmed,

I don't have an answer to your question regarding the non allocation and initialization of resultFraction. I just wanted to ask you if your code is also printing the 3/4 twice? In my code its only once but in the book its twice.  Maybe someone else could explain this?

Daniel

it prints once
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.