www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - gaming pointers

I know this is just playing dangerous, still tempting to use for things like
plugins ,"member interfaces" or even struct interfaces.
It is easy to do without unions because delegates have ptr and funcptr pointers
with just a swap of pointers.
Are .ptr and .funcptr really needed to be writeable?
"could be a warning" (:

Here a example (note that the union it is used only to pretty print)

module gccmembug;
import std.stdio;

union dlgtrick(T){
    void delegate() aptr;
    struct{
        T* cptr;
        void* mptr;
    }
}

class bypassing{
    private int x;
    void outx(){writeln(x);}
}
class dump{
    int x;
    void changex(){
        x +=2;
    }
}
void gamingboard(){
    dump tool = new dump();
    bypassing instance = new bypassing();
    dlgtrick!bypassing drill;
    drill.aptr = &instance.outx;
    writeln("real bypassing \t",drill.cptr," ",drill.mptr);
    drill.aptr = &tool.changex;
    writeln("real tool \t",drill.cptr," ",drill.mptr);
    drill.aptr.ptr = (&instance.outx).ptr;
    writeln("faked \t",drill.cptr," ",drill.mptr);
    writeln("original function called : ");
    instance.outx();
    writeln("hacked version called :");
    drill.aptr();
    instance.outx();
}

Excuse my if I maade something obvious or silly.
Thanks g.
Dec 28 2010