views:

74

answers:

3

I'm reading in my text book about virtual functions in C++ and my book doesn't elaborate on what exactly run-time binding is. It doesn't give me any information as to why I would need run-time binding.

Maybe the nice people at SO can provide me with some links or information?

Thanks :]

+2  A: 

How about this one? ;D

http://www.google.ca/search?hl=en&source=hp&q=virtual+function+runtime+binding+c%2B%2B&aq=f&aqi=&aql=&oq=&gs_rfai=

In all seriousness though... the first link looks decent.

Here's a preview:

The most prominent reason why a virtual function will be used is to have a different functionality in the derived class. The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time.

And from another site:

In large, complex programs, virtual functions allow the programmer to simplify the programming process. If used correctly, the base class will successfully define the interface of its derived classes. However, the program will leave the implementation of this interface up to the derived classes. Therefore, the programmer can use one interface for multiple implementations. This capability also allows for the creation of class libraries, which establish standard interfaces, but allow the programmer to tailor these interfaces to any unique implementation situations that may arise. One of the most popular libraries around is the Microsoft Foundation Classes (MFC) library, which provides the interfaces necessary for programming in the Windows environment. This library frees the programmer from having to reinvent the Windows interfaces, instead allowing him or her to focus on the specific implementation of these interfaces.

Chris Cooper
Meh, I searched for "virtual C++" and got junk
Ok, well, add a few more key words! Virtual C++ could refer to the practice of simulating the writing of C++! ;D Actually the addition of "runtime binding" really helps.
Chris Cooper
Googling a plain english question often works out better than you might think. :)
Cogwheel - Matthew Orlando
@Cogwheel: Indeed! Google has great search algorithms since all they have to do is look into their database of what-everyone-on-earth-is-thinking-at-this-exact-instant and respond to the question. ;P
Chris Cooper
+1  A: 

The simplest form of run-time binding is polymorphism. In context of C++ polymorphism is achieved through virtual functions. The basic purpose of this is to call methods on instances of derived classes through a pointer or a reference to a base class.

Googling virtual functions should give you plenty of good results on how and why to do this.

Igor Zevaka
A: 

Please read Uncle Bobs articles on the SOLID principles of Object Orientated Design: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

Obviously, they are not about run time binding but they do describe the type of design problems that you are trying to solve which require to use run time binding.

I think the article on the open closed principle brobably best demonstrates (again, the article isn't about run time binding)when you would need to do this: http://www.objectmentor.com/resources/articles/ocp.pdf

David Relihan