Welcome, Guest. Please login or register.
Did you miss your activation email?
February 23, 2017, 02:18:54 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, 5th edition
| |-+ General Discussions
| | |-+ Chapter 7: allocation within a method
Pages: [1] Go Down
Print
Author Topic: Chapter 7: allocation within a method (Read 1472 times)
pif
Newbie
*
Posts: 28


on: July 27, 2015, 04:23:07 AM

Quote
Inside the method, a new Fraction object is allocated and the resulting addition is performed. The result that is stored in the Fraction object result is then returned by the method, where it is then stored in the variable resultFraction.

I don't really understand how does this work. My understanding was that all the variables initialized inside methods work only locally and by extension - any local allocation and initializing would also be wiped out after exiting the method.

Logged
jonr
Full Member
***
Posts: 155


Reply #1 on: September 10, 2016, 04:23:33 PM

This is a good question and one that I never really thought about.  Well, at least in the context of object allocation within a method.  You are correct about how local variables work with regards to their scope.

I think why this is a special case is because in this example the variable (object) is a pointer.  So, it's creating a reference to a memory location, not merely assigning a value to a variable.  When you create a 'regular' variable in a method or function and initialize it, you are just assigning a value to the variable and not working at the memory address level.

I may not be doing the best job explaining this; probably someone more experienced could do a better job. 

-jonR
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.