Amazon.com Widgets +(Fraction *) allocF
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 22, 2014, 05:13:58 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
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 10 - More on Variables and Data Types
| | | |-+ +(Fraction *) allocF
Pages: [1] Go Down
Print
Author Topic: +(Fraction *) allocF (Read 2415 times)
KwangBok
Newbie
*
Posts: 9



Email




on: January 18, 2010, 08:01:08 AM

Hello!

I'm confused using -, + before methods...

I know '+' is something done to Class like making a new car..

on page 237
why do I have to use '+' 

+(Fraction *) allocF
{
extern int gCounter;
++gCounter;
return [Fraction alloc];
}

I'm confused about when I have to use '+' and '-'
Would you kindly explain more about on this??

and the sign '+' and '-' actually do something? or is it just the sign to let others know..
like comment?

Thanks



Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: January 18, 2010, 08:49:59 AM

The + or the - indicates a class or instance method.   With a class method, the message is sent to the name of the class itself, as in [Fraction allocF] or [Fraction alloc].   With an instance method, you are sending a message to act on a particular object (i.e., instance from the class)  The two alloc methods must be class methods, since you are asking to create a new instance from the class (think about it, if it were an instance method, what instance would you send that message to?).

Hope this helps.

Cheers.

Steve
Last Edit: December 27, 2010, 06:39:49 AM by skochan Logged
KwangBok
Newbie
*
Posts: 9



Email




Reply #2 on: January 19, 2010, 03:14:51 AM

Thank you Steve!

I kind of understand the first one..
However, the second one which counts how many times the object is called had no problem with '-'
and I can't find why it has to be class method.. it just returns the gCounter value..

Why it has to be '+' for the +(int)gCounter part??
Thank you

Last Edit: January 19, 2010, 03:16:29 AM by KwangBok Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: January 19, 2010, 06:06:00 AM

How would you call the allocF method if it were an instance rather than a class method?  What instance (i.e, object) would you send the message to?  Remember that the purpose of the method is to count the number of allocated objects and also to allocate a new Fraction object.

Cheers,

Steve Kochan
Logged
KwangBok
Newbie
*
Posts: 9



Email




Reply #4 on: January 19, 2010, 06:58:45 AM

Hello Steve!

You wake up so early!!

I think you got me wrong, now I understand the first part of the code which was
+(Fraction *) allocF
{
extern int gCounter;
++gCounter;

return [Fraction alloc];
}

but the second part.. the method which counts how many times +(Fracion *) allocF is called.
+(int) count
{
extern int gCounter;
return gCounter;
}

Here ,this method returns the value of gCounter and the receiver is present. Can I just use '-' before this method?? why it has to be '+' .. I'm totally confused because of this part.

Thanks
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: January 19, 2010, 09:51:33 AM

You could make it an instance method, however, its functionality is not related to any particular object.  Therefore, it just makes more sense to make it a class method, in which case you ask the class how many objects it allocated, rather than a particular instance of the class.

Cheers,

Steve
Last Edit: December 27, 2010, 06:40:18 AM by skochan Logged
KwangBok
Newbie
*
Posts: 9



Email




Reply #6 on: January 22, 2010, 02:46:59 PM

Thank you Steve

It really helped!!

just one more thing

The sign '-' and '+' before method name, what does it tells to compiler??
I assume it tells whether it's class method or not.

however, it still works whether I put '-' and '+' before (int)count method.
I am curious what '-' and '+' actually tells to compiler..

Thank you
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #7 on: January 22, 2010, 04:32:01 PM

Yes, it identifies the method as a class (+) or instance method (-).   In the former, the message can only be sent to the class.  In the latter, the message can only be sent to instances from the class.  Note that you can have two methods with the same name in the same class; one can be a class method and the other an instance method.

Cheers,

Steve Kochan
Logged
KwangBok
Newbie
*
Posts: 9



Email




Reply #8 on: January 23, 2010, 04:27:40 AM

Thank you Steve! Have a good one!
Logged
razor
Newbie
*
Posts: 12






Reply #9 on: December 20, 2010, 03:12:25 PM

Thanks for the explanation.  I was looking through the book and couldn't find the answer.  If it is please excuse the suggestion of adding something this discussion in the next revision of the book.
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.