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.

Reply via email to