Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 What are the advantages of statically bound methods, over dynamically bound?
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Wed Jun 09, 2010 9:15 pm   Post subject: What are the advantages of statically bound methods, over dynamically bound?

I'm not completely sure I'm using the right terms here, but what I mean by statically bound is how c++ binds methods by default, and dynamically bound is how c++ binds when using the virtual keyword

I always find it weird how popular c++ is, I like objective-c a lot more, and it's not just because I'm a mac user.

C++ just seams to me to be archaic, with it's static method binding. Everyone is still using it, and I don't get it, I can't think of any reason that static binding is better than dynamic binding. There's surely some advantage?

Well, first of all, I acknowledge that static binding is faster than dynamic binding. In C++, methods are supposedly called as fast as functions (which I believe), whereas objective-c needs to look up the function in the object's method list at execution time, possibly having to move up the class hierarchy, which of course is a lot slower. BUT, once this has been done, it is cached in the object, and subsequent calls are supposedly just slightly slower than calling functions. It's obviously not a big issue, because there are large applications written in objective-c, and every iPhone application is, so it's not even an issue on mobile devices (and on the android, they're written in java, which I'm pretty sure is also dynamically bound[citation needed]).

On the other hand, dynamic binding has much better (real) advantages, first of all, it allows objects with unknown types. In objective-c, you can cast any object to the type id, and pass it to any function that accepts an id, and that function can call methods on it, even though it doesn't know the type. C++ cannot dream of any of this. The closest it can do is with templates, which have two huge drawbacks; they need to be compiled for every type that uses them, and they have to be compiled into the file that is using them (if you use a template in a library, you have to put the full definition in a header file).

Dynamic binding also allows polymorphism. C++ pretends it has polymorphism, but it doesn't. Polymorphism means that an object acts like another object. If in C++, I create a shape class, and create an area function, that would be redefined in the subclasses for specific shapes, I would have to know the exact type to figure out the area, or else it would call area() on shape, and not on, say, square. This can be fixed by marking individual methods as virtual, but that means you have to go and do that. When you create a method, you don't necessarily know if you'll want to redefine a function in a subclass later, it'll be a pain to have to go back and change it later.

So surely there's some reason I'm missing that C++ (and other forms of static binding) is still popular?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jun 09, 2010 9:40 pm   Post subject: RE:What are the advantages of statically bound methods, over dynamically bound?

http://en.wikipedia.org/wiki/Static_code_analysis is one of the main arguments in favour.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Fri Jun 11, 2010 3:26 am   Post subject: RE:What are the advantages of statically bound methods, over dynamically bound?

Disclaimer: this is purely opinion.

You are looking at a fundamental difference in the mindset that brought two languages into existence.

C++ is *not* a pragmatic language. Some will likely be up in arms about that, saying they find C++ quite useful. Well, yes, it can be useful, but that's not why C++ came to be the way it is, nor why it is continuing to develop. When you look at C++, you see a language designed as it was because someday someone somewhere might find a use for its myriad features.

Objective-C is designed with a much tighter set of goals: bring object-orientation to C and create a usable environment for developing graphical applications for end-users. There is no development of pie-in-the-sky features that might someday be useful.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: