views:

52

answers:

2

Hi,

I assume according to "Cocoa design patterns" book i'm reading that the retain function is implemented using something like this :

   - (int)retainCount
// Returns the receiver's current reference count
{
int result = 1; // receiver not in table, its count is 1
void *tableValue = NSMapGet(
[[self class] _myRefCountMapTable], self);
if(NULL != tableValue )
{ // if receiver is in table, its count is the value stored
result = (int)tableValue;
}
return result;
}

- (id)retain
// Increases the receiver's reference count
{
// store the increased value in the table
NSMapInsert([[self class] _myRefCountMapTable], self,
(void *)([self retainCount] + 1));
return self;
}

As the example imply every reference object has the same self member. How does that happen ? maybe I don't understand the meaning of self - I though it's like "this" in C++.

If I just use assignment operator (A=B) Does it copy the pointer(self) and that's it ? I though it would use "copywithzone" and it's relatives and the "self" members won't be equal. Moreover, I though copywithzone is like copy constructor in c++.

I guess i'm confusing between the 2 worlds.

+1  A: 

I remember having heard that you shouldn't make any assumptions on the retainCount. :-)

self is indeed very similar to this.

Assignment just copies the pointer, and it's the same in C++.

NSObject *objA =[[NSObject alloc] init];
NSObject *objB = objA;

objA and objB reference the same object.

Not that your code example uses [self class], so they'd use one table per class for all instances of that class.

Eiko
+1  A: 

As the example imply every reference object …

There is no such thing as a “reference object”. I suspect that's not what you meant, so please clarify.

has the same self member.

Objects do not have members (instances have instance variables, which are similar in concept but not the same in implementation).

self is not a “member”, nor is it an instance variable. Note that classes have self as well. self is a special hidden argument to the message, containing the object that is the receiver of the message.

And no, self does not refer to every object at once. If you send the same message to two different objects, even of the same class, the self argument will contain a different pointer in each message.

maybe I don't understand the meaning of self - I though it's like "this" in C++.

As I understand “this”, yes. self is the object that received the message—in your examples, the object that something is retaining or asking the retain count of.

If I just use assignment operator (A=B) Does it copy the pointer(self) and that's it ?

The pointer copied will only be self if B is self. That is, if you say A = self, then it will copy the self pointer to A. If you say B = self and then you say A = B, same thing, since B and self contain the same pointer. If you had not said B = self, then B is probably some other value, so that other value is what will be copied to A. And that's assuming A and B are pointer variables.

It will copy the value (pointer) you tell it to copy. Nothing else.

I though it would use "copywithzone" and it's relatives and the "self" members won't be equal.

No. The object is only sent a copyWithZone: message (do not omit colons—they are significant) when something sends it a copyWithZone: message. The most common way is to send it a copy message, as that will send a copyWithZone: message in turn.

Furthermore, even a “copy” does not always copy the object. Immutable objects can implement copyWithZone: to return [self retain] instead.

However, plain assignment never copies the object. It only copies the pointer.

Moreover, I though copywithzone is like copy constructor in c++.

Roughly. I don't know enough C++ to say how much like it it is.

Peter Hosey
Like the scenario in the commennt above yours, calling [objB retain] and calling [objA retain] will contain different "self" parameter as you said, which will make my example of "retain" function incorrect.So who's wrong ? my understanding ? Cause i'm pretty sure the example is correct.I just means that assignment like "SObject *objB = objA;" will cause both objects' retain function to have the same "self" when called (or any other function)
Idan
Yes, your understanding. See the last paragraph of Eiko's answer for what happens in your `retain` implementation. When you assign `objB = objA`, there are no “both objects”; there is one object, whose pointer is in two variables. In that case, `[objA retain]` and `[objB retain]` will both send the `retain` message to the same object, which means they will both run the same implementation of `retain`, and that implementation will receive the same value of `self` both times.
Peter Hosey
Great, that's pretty much what I was thinking, as you can see from my last comment line above. Thanks alot!
Idan