Amazon.com Widgets Very confused about some aspects of chapter 8
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 19, 2013, 02:31:51 PM
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
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 8 - Inheritance
| | | |-+  Very confused about some aspects of chapter 8
Pages: [1]   Go Down
Print
Author Topic: Very confused about some aspects of chapter 8  (Read 381 times)
GreatPumpkin
Newbie
*
Posts: 12






« on: September 12, 2011, 02:50:20 AM »

Hi, I'm a total newbie and although it's been a struggle on occasions I've got this far but now feel really stuck.

On page 164 location 3741: I'm confused about the meaning of:

    -(XYPoint *) origin;

or

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

I don't get why the origin method returns a variable of type "XYPoint *" or why setOrigin receives a variable of type "XYPoint *".

What does this mean and what does the asterisk mean?  The book said the asterisk was important but I didn't really understand why or what it signifies.

I don't understand why origin is pointing at the same location in memory as XYPoint while previous statements such as x = y where x and y are integer variable will simply set the value of x to the value of y.

If anyone can offer any help or point me in the direction of a suitably simple explanation I'd be so grateful.  Thanks.
« Last Edit: September 12, 2011, 04:03:24 AM by GreatPumpkin » Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #1 on: September 12, 2011, 08:03:12 AM »

I would appreciate a bit more of code, but i'll try and answer what i think your problem is Smiley

In your interface file, you have declared a variable to hold an XYPoint reference, like this:
Code: (Objective-C)
[b]Rectangle.h[/b]
{
   XYPoint *origin;
}

This means, that you have created a variable named "origin" and that it will hold a reference, or pointer to an XYPoint object in memory.

So far so good. Now, notice that you haven't done the alloc and init method on this object, meaning that right now it's just an empty variable. You need to have it point to an object in memory, thus you need to create an object. This is why you pass in an object as an argument, like this:
Code: (Objective-C)
-(void) setOrigin: (XYPoint *) pt;
This makes you create and allocate and initialize an XYPoint object in your main routine. By passing that same object in as an argument, you make your origin variable point / reference the same object in memory. Thus, right now both the object of an XYPoint class you declared in main, and the origin variable holds a reference to the same object.

Now you can then do operations on it, as you actually have an allocated and initialized object, in an instance of another class. You can set the x and y of the origin object, just as if it was in your main routine (although you need to create methods to do so).

If you are unclear about the whole objects reference and pointing thing, let me know and we will take it from there Smiley

Hope i helped!

Best regards
/JBJ
www.jbjprogramming.com
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
GreatPumpkin
Newbie
*
Posts: 12






« Reply #2 on: September 12, 2011, 08:53:05 AM »

Thanks very much for your reply.  I am still confused but I'll work through your answer a few times and the section in the book to try to get to grips with it. Hopefully something will fall into place but if not I'll let you know!

Much appreciated.
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.