Get a Reference to an Object's Monitor

Timon Gehr timon.gehr at gmx.ch
Tue Dec 20 15:50:49 PST 2011


On 12/21/2011 12:40 AM, bearophile wrote:
> Andrew Wiley:
>
>> I suspect the reason they advise not using it in user code is that the
>> monitor is lazily initialized (if I remember correctly), so I'd have
>> to initialize it myself.
>
> I see.
>
> I have a question myself. Regarding this, do you know why __vptr is an instance attribute instead of a class attribute?
> http://d.puremagic.com/issues/show_bug.cgi?id=3939
>
> Bye,
> bearophile

The vptr is used to implement dynamic method calls. Therefore it might 
not correspond to the static type of the reference and has to be an 
instance attribute.
Why would you have to manipulate vptr? You can use typeid to compare 
instances for identity of their corresponding type.
Anyway, to get the vptr of a class C, do this:

void** vptr = typeid(C).vtbl.ptr;


More information about the Digitalmars-d mailing list