struct opCast to void* and back
Nrgyzer
nrgyzer at gmail.com
Sun Apr 11 07:41:29 PDT 2010
Robert Clipsham Wrote:
> On 11/04/10 12:47, Nrgyzer wrote:
> >> 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"
>
> I don't know how writefln() works in D1 (I presume that's what you're
> using), but try the following:
>
> writefln( "%s", (*cast(MyStruct*)temp).toString() );
>
> I don't have a D1/Phobos compiler available to me to test, so I don't
> know if that will help. You usually get that error when trying to print
> /use strings with invalid UTF sequences in them, which looking at your
> program, shouldn't be happening. If you're using code other than what
> you're showing that could be it.
I want to do the following:
struct MyStruct {
char[] structName;
public char[] toString() {
return structName;
}
}
...
class MyClass {
private static void function(void*) callBack;
private static void* callBackValue;
...
static this() {
MyStruct test = MyStruct();
test.structName = "Struct name";
callBack = &cb;
callBackValue = &test;
}
...
public void cb(void* value) {
MyStruct temp = *((cast(MyStruct*) value));
writefln(temp);
}
}
More information about the Digitalmars-d
mailing list