Re: Mixing Obj-C and C "methods"
Re: Mixing Obj-C and C "methods"
- Subject: Re: Mixing Obj-C and C "methods"
- From: Andy Lee <email@hidden>
- Date: Wed, 31 Jul 2013 02:51:36 -0400
On Jul 30, 2013, at 5:29 PM, Michael Crawford <email@hidden> wrote:
> That class object occupies a
> non-zero quantity of memory, at its lowest level being somewhat like
> the combination of a single copy of a C struct, as well as some C
> functions, that from the Objective-C point of view, appear to be
> "class methods" that know all about their "class object",
Both class methods and instance methods are indeed C functions (IMPs). But to clarify, it isn't that class methods "know" about their owning class in the sense one might imagine, with some sort of back-pointer. It's that at the time a class method is invoked, a class pointer is passed to the function as an implicit argument named "self", and *that's* how the function knows the class on whose behalf it is executing. It's a subtle distinction, but an important one.
One reason it's important is that you could have a subclass and it could invoke the same class method:
[MyClass myClassMethod];
[MySubclass myClassMethod]; // Assume MySubclass does not override the method.
The value of "self" will be different in the two cases at the time that method executes. In each case, "self" is the receiver of the message.
Similarly, at the time an instance method is invoked, an instance pointer is passed to the function that constitutes the implementation of the method, again as an implicit argument named "self".
> but that are
> not capable of (directly) operating on instances.
All functions, whether pure C functions or Objective-C methods, are capable of directly operating on instances. They can instantiate objects, message them, and depending on scope, they can directly access their ivars.
The distinction you may be getting at is that only Objective-C instance methods can directly access ivars without having to qualify them with "self->", because the "self" is implicit.
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden