&this pointer

Brian Byrne bdbyrne at wisc.edu
Sun Feb 11 12:30:36 PST 2007


e-t172 wrote:
> The following does not work :
> 
> import std.stdio;
> 
> class Test
> {
>     this()
>     {
>         void* ptr = cast(void*) this;
> 
>         (*(cast(Test*) ptr)).foo();
>     }
> 
>     void foo()
>     {
>         writefln("OK");
>     }
> }
> 
> int main()
> {
>     auto test = new Test();
> 
>     return 0;
> }
> 
> Result : segmentation fault
> 
> Could one explain why ?

"this" is of type "Test". Therefore to recover it from a void*, do simply:

(cast(Test)ptr).foo();


Brian Byrne


More information about the Digitalmars-d-learn mailing list