Casting pointers

Deewiant deewiant.doesnotlike.spam at gmail.com
Sat Jul 7 08:21:35 PDT 2007


Frits van Bommel wrote:
> First cast to a C* (or Object*), then dereference it and cast to I.
> These'll probably work:
> ---
>     I i = cast(I) *cast(Object*) &c;
> // and
>     I i = cast(I) *cast(C*) &c;
> // though cast(C*) is pretty useless here :)
> ---

As you note, cast(C*) is useless, so it boils down to:

I i = cast(I) *&c;

Which boils down to:

I i = cast(I) c;

Which boils down to:

I i = c;

(No cast needed since C inherits from I.)

So you've managed to sidestep the whole point, in a way. Although I'm not really
sure what the point was, and why one would need a pointer to an interface.

Casting to an Object is equivalent, but it's probably slower since it has to do
a dynamic cast. Contrast the Object version:

L0: push    EAX
    push    offset FLAT:_D4asdf1I11__InterfaceZ
    push    offset FLAT:_D4asdf1C7__ClassZ
    call    near ptr __d_newclass
    add     ESP,4
    push    EAX
    call    near ptr __d_dynamic_cast
    add     ESP,8
    mov     ECX,[EAX]
    call    dword ptr 4[ECX]

With the other versions:

L0:  push    EAX
     push    offset FLAT:_D4asdf1C7__ClassZ
     call    near ptr __d_newclass
     add     ESP,4
     test    EAX,EAX
     je      L17
     lea     EAX,8[EAX]
     jmp short L19
L17: xor     EAX,EAX
L19: mov     ECX,[EAX]
     call    dword ptr 4[ECX]

(Retrieved with obj2asm after dmd -O.)

-- 
Remove ".doesnotlike.spam" from the mail address.



More information about the Digitalmars-d mailing list