我很驚訝地發現,浮點值可以被Delphi中的Variant分開。什麼可以做一個簡單的例子:爲什麼我可以在Delphi中將Single by Variant分開,它是幹什麼用的?
var
v: Variant;
begin
v := 2.3;
Tag := 5.1 div v; // 2
Tag := 5.1 mod v; // 1
Tag := 5.1 div 2; // [dcc32 Error] E2015 Operator not applicable to this operand type
Tag := 5.1 mod 2; // [dcc32 Error] E2015 Operator not applicable to this operand type
end;
它看起來像德爾福輪左部和右部做div
/mod
操作之前。
我期望上面的代碼在編譯時在所有4行中產生錯誤,因爲我的理解是div/mod無論如何都不適用於浮點值。顯然情況並非如此。
爲什麼我可以在Delphi中將Single by Variant分開,它需要什麼?
這只是變種強制。變體旨在允許儘可能多的類型轉換。如果你想了解更多關於爲什麼那麼你應該看看MS COM文檔。 –