Monday, November 8, 2010

Dynamic Dispatching

You cannot do dynamic dispatching on a value type parameter using a virtual function call. You can do it on a pointer to an object, and a reference to an object.

Trying to do dynamic dispatching using 'pass by value', typically gives you a slicing problem, where the extra data added by derived class is stripped off the object as it comes into the function. Also, the function called will be the base function instead of the expected derived function.

For dynamic dispatching, there are two types, the Static Type (e.g. Base * pBase;) and the Dynamic Type, the type used in the new, i.e. constructor (e.g. Base * pBase = new Derived). Depending on which functions are virtual and how you pass you object, the function you call will be based on either the Static Type or the Dynamic Type.

No comments:

Post a Comment