www.digitalmars.com         C & C++   DMDScript  

D - Are virtual tables really required?

reply parabolis <parabolis_member pathlink.com> writes:
Why would a language with only single inheritence need virtual tables?

Given the following code:

class Object
{  void print()
char[] toString()
uint toHash()
int cmp()
}

class DerivedEx : Object
{           void someFunc1()
override uint toHash()
override  int cmp()
void someFunc2()
}

And assuming a heap representation of a class is something like this:
(which is very close to class ClassInfo in object.d)

struct ClassInfo
{   void* superClassPtr;
uint  funcPtrTableLength;
void* funcPtrTable[0];
..
void* funcPtrTable[funcPtrTableLength - 1];
..  (other info)
}

Then the Object class on the Heap will be:

struct ClassInfo
{   void* superClassPtr = 0;
uint  funcPtrTableLength = 4;
void* funcPtrTable[0] = addrOf(print);
void* funcPtrTable[1] = addrOf(toString);
void* funcPtrTable[2] = addrOf(toHash);
void* funcPtrTable[3] = addrOf(cmp);
..  (other info)
}

and DerivedEx class will be represented in the Heap:

struct ClassInfo
{   void* superClassPtr = addrOf(Object ClassInfo);
uint  funcPtrTableLength = 6;
void* funcPtrTable[0] = addrOf(print);
void* funcPtrTable[1] = addrOf(toString);
void* funcPtrTable[2] = addrOf(DerivedEx.toHash);
void* funcPtrTable[3] = addrOf(DerivedEx.cmp);
void* funcPtrTable[4] = addrOf(someFunc1);
void* funcPtrTable[5] = addrOf(someFunc2);
..  (other info)
}

In this example the funcPtrTable for a Derived Class is first populated by the
contents of its super class. Then any function that overriddes another simply
replaces the appropriate slot in the funcPtrTable. Finally the table is expanded
to hold any other functions defined by the Derived Class. An abstract class
which does not implement a function would allocate space for it but set it to 0.
Jul 23 2004
parent J C Calvarese <jcc7 cox.net> writes:
parabolis wrote:
 Why would a language with only single inheritence need virtual tables?
 
FYI This is the "old" newsgroup. Please post "new" threads in the one of the "new" newsgroups. *General D Discussions* Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D Newsgroup: news://news.digitalmars.com/digitalmars.D *Bug Reports for DMD* Web: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs Newsgroup: news://news.digitalmars.com/digitalmars.D.bugs *D GNU newsgroup* Web: http://www.digitalmars.com/drn-bin/wwwnews?D.gnu Newsgroup: news://news.digitalmars.com/D.gnu *Old D newsgroup* (don't post here anymore!) Web: http://www.digitalmars.com/drn-bin/wwwnews?D Newsgroup: news://news.digitalmars.com/D
 Given the following code:
 
 class Object
 {  void print()
 char[] toString()
 uint toHash()
 int cmp()
 }
 
 class DerivedEx : Object
 {           void someFunc1()
 override uint toHash()
 override  int cmp()
 void someFunc2()
 }
 
 And assuming a heap representation of a class is something like this:
 (which is very close to class ClassInfo in object.d)
 
 struct ClassInfo
 {   void* superClassPtr;
 uint  funcPtrTableLength;
 void* funcPtrTable[0];
 ..
 void* funcPtrTable[funcPtrTableLength - 1];
 ..  (other info)
 }
 
 Then the Object class on the Heap will be:
 
 struct ClassInfo
 {   void* superClassPtr = 0;
 uint  funcPtrTableLength = 4;
 void* funcPtrTable[0] = addrOf(print);
 void* funcPtrTable[1] = addrOf(toString);
 void* funcPtrTable[2] = addrOf(toHash);
 void* funcPtrTable[3] = addrOf(cmp);
 ..  (other info)
 }
 
 and DerivedEx class will be represented in the Heap:
 
 struct ClassInfo
 {   void* superClassPtr = addrOf(Object ClassInfo);
 uint  funcPtrTableLength = 6;
 void* funcPtrTable[0] = addrOf(print);
 void* funcPtrTable[1] = addrOf(toString);
 void* funcPtrTable[2] = addrOf(DerivedEx.toHash);
 void* funcPtrTable[3] = addrOf(DerivedEx.cmp);
 void* funcPtrTable[4] = addrOf(someFunc1);
 void* funcPtrTable[5] = addrOf(someFunc2);
 ..  (other info)
 }
 
 In this example the funcPtrTable for a Derived Class is first populated by the
 contents of its super class. Then any function that overriddes another simply
 replaces the appropriate slot in the funcPtrTable. Finally the table is
expanded
 to hold any other functions defined by the Derived Class. An abstract class
 which does not implement a function would allocate space for it but set it to
0.
-- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Jul 23 2004