Robert Clipsham Wrote:

> On 11/04/10 10:46, Nrgyzer wrote:
> > Hello everyone,
> >
> > how can I cast a struct to void* and back to my struct? I have the 
> > following struct:
> >
> > struct myStruct {
> >
> >     char[] structName;
> >
> >     public char[] toString() {
> >             return structName;
> >     }
> >
> >     void* opCast() {
> >             return&this;
> >     }
> >     
> > }
> >
> > myStruct test = myStruct(); // ok
> > test.structName = "Example struct"; // ok
> > void* temp = cast(void*) test; // ok
> > writefln(cast(myStruct) temp); // failed
> >
> >
> > Thanks for solutions&  help :).
> 
> Try *cast(MyStruct*). In opCast you are returning &this, which is of 
> type MyStruct* until it is implicitly cast to void*. In the writefln() 
> you then cast it back to MyStruct*, and dereference it so you can use 
> it. In your version you are trying to cast from a pointer to the struct 
> to the actual struct, which is why it doesn't work.

By using *cast(MyStruct*) I get the following message: "Error: 1invalid UTF-8 
sequence"

Reply via email to