Skip to main content
GameDev.net gamedev.net
🔒 Locked

Objective-C

Started by yaroslavd Jan 6, 2009 at 9:26 AM 8 replies 2k views
Original Post
yaroslavd
yaroslavd
I've been looking at Objective-C and I am failing to understand the fundamental difference between C++ OO and Objective-C/Smalltalk OO. I understand that in Obj-C you pass messages to objects instead of calling methods directly, but what is the difference besides semantics? I know that there is something I'm missing.
SiCrane
SiCrane
Wikipedia's entry on Objective-C has a fairly good discussion of the differences of the object models. But, in a nutshell, it's basically the difference between static typing and dynamic typing. In C++, you can't call a function on an object unless you're sure at compile time that object supports that function. In Objective-C, you can send a message to an object even if at compile time that can't be determined that the object will process that message.
yaroslavd
yaroslavd
Yeah, I read the Wikipedia entry, which is where I read up on the differences. I just couldn't really understand how these differences impact the programmer. Thanks for helping clarify it, though.

So would you say that Obj-C OO is similar to parametric polymophism a la SML? Where you can pass in any object at will perform the method as long as the type has that method?
arbitus
arbitus
Quote:
Original post by yaroslavd
Yeah, I read the Wikipedia entry, which is where I read up on the differences. I just couldn't really understand how these differences impact the programmer. Thanks for helping clarify it, though.

So would you say that Obj-C OO is similar to parametric polymophism a la SML? Where you can pass in any object at will perform the method as long as the type has that method?


Unless I am missing something, that is what dynamic typing is, and Objective-C is dynamic.
SiCrane
SiCrane
It's been literally over a decade since I've used Standard ML, but from what I recall, it is different. SML has static typing with parametric polymorphism. You still need to determine at compile time that an object supports a given operation, it's just that type inference means that the same function can be applied to different static types and perform different things for different types based on their types. However, when talking about Objective C, you're dealing with actual runtime dynamic typing: an message can be sent to an object whose interface lacks a handler for that message at compile time.
krez
krez
Quote:
Original post by SiCrane
However, when talking about Objective C, you're dealing with actual runtime dynamic typing: an message can be sent to an object whose interface lacks a handler for that message at compile time.

What happens then?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
stonemetal
stonemetal
What happens is called forwarding. In GNUStep the function forwardInvocation is called and passed an object that contains the function name(called a selector) and parameters. Otherwise doesNotRecognizeSelector is called which raises an exception.
yaroslavd
yaroslavd
I just finished the Wikipedia article for the second time. I am still a bit fuzzy on how polymorphism works in Obj-C. Do superclasses forward messages to subclasses? Other way around? Of course in C++ if you call SuperClass.someMethod() it will delegate appropriately to SubClass.someMethod(). Does the same thing happen in Obj-C?
Oluseyi
Oluseyi
Quote:
Original post by yaroslavd
I just finished the Wikipedia article for the second time. I am still a bit fuzzy on how polymorphism works in Obj-C. Do superclasses forward messages to subclasses?

No, the classes are not involved. This is about the objects - the instances of the classes. A class specifies that an object of its type responds to a set of selectors (message names and parameters). At compile time, however, we don't know the type of the object. Oh, sure, we know the type of the pointer we're holding, which allows us to make assumptions about what selectors are available and will be responded to, but only at runtime can the language resolve the true type of the object instance.

Think of it this way: imagine if all classes in C++ were part of a single hierarchy, but dynamic_cast neither threw an exception nor returned null when you cast between "unrelated" (more correctly, indirectly related) types. If A and B both inherit from Object and you tried to call methods of B on an instance of A via a pointer-to-member function, only at runtime would you determine that you had the wrong mix of type and method.

Quote:
Of course in C++ if you call SuperClass.someMethod() it will delegate appropriately to SubClass.someMethod(). Does the same thing happen in Obj-C?

If a parent type and a child type both implement a selector and you hold a child type instance via a parent type pointer, yes, the child implementation will respond.
smc
smc

This is a good tutorial for C++ programmers.
∫Mc

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.