Wednesday, July 29, 2009

little tidbit on polymorphism in C++

Here's another interview question that I've come across:

Polymorphism in C++ is supported with the use of dynamic binding with the use of virtual functions.

There are two things to do when you want to use dynamic binding:

  1. Only member functions that are declared virtual are dynamicallly bound.
  2. The call must be made through a pointer or reference to a base class.

When these conditions are met, the function call is dynamically bound. Otherwise, the function is resolved at runtime. And dynamic binding is how C++ supports polymorphism. (Although I believe templates support a different kind of polymorphism, dynamic binding polymorphism is what people mean when they simply say polymorphism...)

No comments: