classInstanceSize and vtable
Simen Kjaeraas via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 23 23:33:33 PDT 2014
On Friday, 24 October 2014 at 00:21:52 UTC, Etienne Cimon wrote:
> On 2014-10-23 20:12, bearophile wrote:
>> In D all class instances contain a pointer to the class and a
>> monitor
>> pointer. The table is used for run-time reflection, and for
>> standard
>> virtual methods like toString, etc.
>>
>> Bye,
>> bearophile
>
> So what's the point of making a class or methods final? Does it
> only free some space and allow inline to take place?
Like bearophile said the vtable is required for virtual methods.
Consider this code:
import std.stdio : writeln;
class A { void foo() {writeln("A");} }
final class B : A { override void foo() {writeln("B");} }
void main() {
A a = new B();
a.foo();
}
In order for the call to foo to run the correct version of foo, B
needs to have a vtable. Since all classes in D implicitly inherit
from Object, which has some virtual methods, all classes need to
have a vtable.
--
Simen
More information about the Digitalmars-d-learn
mailing list