Pro Multithreading and Memory Management for IOS and OS X: With ARC, Grand Central Dispatch and Blocks by Kazuki Sakamoto & Tomohiko Furumoto

Pro Multithreading and Memory Management for IOS and OS X: With ARC, Grand Central Dispatch and Blocks by Kazuki Sakamoto & Tomohiko Furumoto

Author:Kazuki Sakamoto & Tomohiko Furumoto [Sakamoto, Kazuki & Furumoto, Tomohiko]
Language: eng
Format: epub
Tags: Computers, Programming, Apple Programming, Object Oriented
ISBN: 9781430241164
Publisher: Apress
Published: 2012-04-25T04:00:00+00:00


By the C++ compiler, it is converted to a source code calling the C function as follows.

struct MyClass cls;

__ZN7MyClass6methodEi(&cls, 10);

"this" is an instance of MyClass class (struct) itself. In addition, let's check an

instance method in Objective-C.

- (void) method:(int)arg

{

NSLog(@"%p %d\n", self, arg);

}

The same as for C++ methods, the Objective-C compiler converts the method to a C

function.

void _I_MyObject_method_(struct MyObject *self, SEL _cmd, int arg)

{

NSLog(@"%p %d\n", self, arg);

}

And the same as “this” in C++, “self” is passed as the first argument. Let’s see its caller as well.

MyObject *obj = [[MyObject alloc] init];

[obj method:10];

Using “-rewrite-objc option” for clang, we can see how it is converted:

MyObject *obj = objc_msgSend(

objc_getClass("MyObject"), sel_registerName("alloc"));

obj = objc_msgSend(obj, sel_registerName("init"));

objc_msgSend(obj, sel_registerName("method:"), 10);

The objc_msgSend function searches a pointer of the _I_MyObject_method_ function by the object and the method name. After that, it calls the function pointer, and the first argument “obj” is passed to the _I_MyObject_method_ function as its first argument “self”. As in C++, “self” is the object of the MyObject class itself.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.