CSC/ECE 517 Fall 2010/ch2 5c gn: Difference between revisions
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
=== Introduction === | === Introduction === | ||
The process of identifying which version of the function with identifier will be executed during the runtime. Dynamic Dispatch generally happens in OOLS. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type the reference points to at that time, instead of type of reference. In literature it also given different names like Runtime Binding or [Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism]. | |||
=== Why we need Dynamic Dispatch === | === Why we need Dynamic Dispatch === | ||
=== How Dynamic Dispatch works === | === How Dynamic Dispatch works === |
Revision as of 02:09, 27 October 2010
Dynamic Dispath
Introduction
The process of identifying which version of the function with identifier will be executed during the runtime. Dynamic Dispatch generally happens in OOLS. For example, we can have a super class which defines a particular function and then there is a subclass which defines the same function (called overriding). When a reference to the super class is used to execute that function, then which version of the function gets execute depends on the type the reference points to at that time, instead of type of reference. In literature it also given different names like Runtime Binding or [Dynamic Polymorphism http://en.wikipedia.org/wiki/Dynamic_polymorphism].
Why we need Dynamic Dispatch
How Dynamic Dispatch works
Dynamic Dispatch working in Detail
class Cat { public: void talk() { cout << "Meow!!"; } }; class Dog { public: void talk() { cout << "Bark!!"; } }; struct Cat { int __id__; // 0 char name[10]; }; struct Dog { int __id__; // 1 char name[10]; }; void cat_talk() { printf("meow!"); } void dog_talk() { printf("Bark!!"); } void talk(void *object) { int id = *(int *)object; vtable[id](); } void (*vtable[2]); vtable[0] = cat_talk; vtable[1] = dog_talk; Cat *c = new Cat(); Cat *c = malloc(sizeof(cat)); c->__id__ = 0;