I need to be able to convert a naked pointer to a variant. I know that the 开发者_JAVA百科pointer points to a variant, but I can't seem to get it back out. A straight cast (as I pretty much thought) fails:
Result := Variant(FAddress)^
returns a compiler error: [DCC Error] E2089 Invalid typecast
I've scoured the variants.pas unit as well, but nothing jumped out at me.
Obviously I'm missing something. What is the way to do this?
If the pointer points at a Variant, then its type is PVariant. Type-cast it to that, and then dereference:
Result := PVariant(FAddress)^;
Better yet, declare FAddress
with the right type to begin with, and then you don't need to typecast:
var
FAddress: PVariant;
Result := FAddress^;
The compiler considers your attempted type-cast invalid because Variant is a bigger type than Pointer is. The compiler doesn't know where to get the additional data to create a full Variant value. And if the type-cast were valid, the use of the ^
operator isn't allowed on Variants anyway. You might have gotten away with this:
Result := Variant(FAddress^);
I've never liked that; if FAddress is an untyped pointer, then dereferencing it yields a value without any size or type at all, and it's just weird to type-cast such a thing.
精彩评论