Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Sunday, August 26, 2007

polymorphism in C

Even polymorphism can by implemented in C. Polymorphism in Java and C++ is implemented by associating with each instance a table of functions. Polymorphic method names are converted to indexes into this function table. When a method call on a particular instance is executed, the function table for that instance (actually these tables are shared among the instances of a class), used to find the proper function to execute.

Since C has the ability to store pointers for functions as a basic data type, and the ability to calla function from such a function pointer. It is straightforward (though not always fun) to explicitly duplicate the function table mechanism and implement polymorphisms by hand.

Al