struct opCast to void* and back
Nrgyzer
nrgyzer at gmail.com
Sun Apr 11 04:47:06 PDT 2010
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"
More information about the Digitalmars-d
mailing list