Re: pointers in objective-c?
Re: pointers in objective-c?
- Subject: Re: pointers in objective-c?
- From: Negm-Awad Amin <email@hidden>
- Date: Sat, 2 Aug 2008 09:09:56 +0200
… and there is a semantical difference:
Changing a pointer does not change the object. changing the object
does not change the pointer.
This is important for encapsulation. Every change of an object made
through a pointer will change the object for every other user of this
object, who refers using a different pointer.
Person* person = …
Person* person2 = person; // two pointers to still *the one and only*
object
person.name = @"Amin"; // person2.name is @"Amin", too!
Amin
Am Fr,01.08.2008 um 22:47 schrieb Michael Ash:
On Fri, Aug 1, 2008 at 2:43 PM, Wayne Shao <email@hidden> wrote:
It seems that every object is a pointer in the sample code I have
seen. Is there any distinction between an object and its pointer?
Others have addressed the general concepts involved, but I wanted to
answer this question specifically.
There is a distinction between an object and its pointer, an important
distinction. An object is a blob of memory with a certain layout. That
layout is determined by the instance variables of the class, including
all of its superclasses. The first item in the blob is always the
'isa' instance variable, which points to the object's class. This is
how the runtime knows what kind of object you're dealing with, even
when you pass things around as 'id'. The remaining items are the rest
of the instance variables.
A *pointer* to an object is just an address, like 0x12345678. This is
the address of the first byte in that blob of memory. A pointer
essentially lets you find that object in memory, but it is not the
object. Think of it as the difference between your friend Bob, and
your friend Bob's phone number. You can store Bob's phone number in
every room of your house, but there remains only one Bob. If you then
give Bob a present, the phone number in every room in the house will
reach the Bob who is pleased because you gave him a present.
Mike
_______________________________________________
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
Amin Negm-Awad
email@hidden
_______________________________________________
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