PDA

View Full Version : Returning a pointer




jwei92
Jun 5, 2011, 03:20 PM
I've looked through a couple threads and am still lost on returning pointers.

I am currently in the process of learning Obj-C with not previous programming background.

I know some basics, but the only thing I cannot seem to grasp is in Kochan's Objective-C 2.0's book (Chapter 8 - A Point Class and Memory Allocation)

I know how to allocate, but here are the parts that confuse me:


@interface Rectangle:NSObject
{
int width, height;
XYPoint *origin;
}

-(XYPoint*)origin;
-(void)setOrigin:(XYPoint*)pt;


I do not understand why the method returns pointers and such. Sorry if my question isn't clear, but can someone elaborate on why you return a pointer and not just an integer?



lee1210
Jun 5, 2011, 03:29 PM
All objects are gotten to in Objective-C via a pointer. That's it. Passing an object? You pass a pointer. Getting one back? Pointer. End of story. If it's just an int, sure... just return the int. There may be some reason to return a pointer to a primitive (maybe you want to modify it and have that affect something else that also uses the value in that memory location), but with objects there is no question.

-Lee

larswik
Jun 5, 2011, 03:38 PM
That was the point I got to in that same book. When he started in with pointers and self it got complicated. I then put that book down and Thought, Objective C is built on top of C. So I got the book "Learn C on the Mac" http://www.amazon.com/Learn-C-Mac-Dave-Mark/dp/1430218096 and that book was pretty easy to follow, this was end of last year.

I have now started back on learning Objective C and from the C book I understand all of the pointer concepts, data structures , functions and so on. Since I liked the Aprees book Learn C on the Mac I decided to start with Learn Objective C for Absolute Beginners by the same company. I just started reading it the other day.

If it gets hard for you, like me, then I recommend taking it a step back to something simpler. This is from experience.

-Lars

Shawnpk
Jun 6, 2011, 10:23 AM
That was the point I got to in that same book. When he started in with pointers and self it got complicated. I then put that book down and Thought, Objective C is built on top of C. So I got the book "Learn C on the Mac" http://www.amazon.com/Learn-C-Mac-Dave-Mark/dp/1430218096 and that book was pretty easy to follow, this was end of last year.

I have now started back on learning Objective C and from the C book I understand all of the pointer concepts, data structures , functions and so on. Since I liked the Aprees book Learn C on the Mac I decided to start with Learn Objective C for Absolute Beginners by the same company. I just started reading it the other day.

If it gets hard for you, like me, then I recommend taking it a step back to something simpler. This is from experience.

-Lars

+1 These are the books I started with. They are much easier to digest than Kochan's book. Kochan's book is a great 3rd book to read.