|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - Are virtual tables really required?
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
parabolis wrote:Why would a language with only single inheritence need virtual tables? Jul 23 2004
|