• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: functions vs methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: functions vs methods


  • Subject: Re: functions vs methods
  • From: Ondra Cada <email@hidden>
  • Date: Fri, 20 May 2005 01:28:06 +0200

Ouch!

On 20.5.2005, at 0:54, Sean McBride wrote:

Given one of my classes, Foo, how can I define a function (not method)
post() which accesses an instance variable textView of Foo? The
following doesn't work:



You might find this thread useful: <http://www.cocoabuilder.com/archive/message/cocoa/2003/7/3/80660>

Rather not! I've read five or six messages from the thread and I regret to say nearly all the writers are patently wrong.


It's quite simple: far as functions per se go, ObjC directives have *no* effect at all. Put a function outside or inside @implementation/ @end, there's no difference *at all*. And, presumed the function is in a .m (or .M, for ObjC++) file, you can freely use messaging inside. Again, regardless @implementation's, even regardless there's any.

The only difference (which has a little to do with functions, it works more or less the same way in methods) is that code inside @implementation/@end is allowed to access the class' protected and private properties directly, whilst code outside of them is not. Thus:

9 /tmp> >q.m
#import <Foundation/Foundation.h>
@interface A {
@public int public;
@protected int protected;
@private int private;
}
@end
void f1(A *a) {
printf("%d",a->public); // OK
printf("%d",a->protected); // Wrong
printf("%d",a->private); // Wrong
}
@implementation A
void f2(A *a) {
printf("%d",a->public); // OK
printf("%d",a->protected); // OK
printf("%d",a->private); // OK
}
@end
10 /tmp> cc -c q.m
q.m: In function 'f1':
q.m:10: warning: instance variable `protected' is @protected; this will be a hard error in the future
q.m:11: warning: instance variable `private' is @private; this will be a hard error in the future
11 /tmp>
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >functions vs methods (From: Roland Silver <email@hidden>)
 >Re: functions vs methods (From: "Sean McBride" <email@hidden>)

  • Prev by Date: 3D surface graphs
  • Next by Date: Re: 3D surface graphs
  • Previous by thread: Re: functions vs methods
  • Next by thread: Obtaining a reference to an entity's context's owner
  • Index(es):
    • Date
    • Thread