Jump to content
  • Advertisement
Sign in to follow this  
yaroslavd

Objective-C

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!